简体   繁体   中英

Get The Product Id of An Item If User Changes Quantity to 0 in WooCommerce Cart

I am trying to figure out a way to get the product ids of items that have their quantity reduced to 0 from the cart page.

I hook into the after woocommerce_after_cart_item_quantity_update which fires if user changes quantity to anything BUT 0. :(

function action_woocommerce_after_cart_item_quantity_update( $cart_item_key, $quantity, $old_quantity ) {
    var_error_log([$cart_item_key, $quantity, $old_quantity]);
};

add_action( 'woocommerce_after_cart_item_quantity_update', 'action_woocommerce_after_cart_item_quantity_update', 10, 3 );


function var_error_log( $object=null ){
    ob_start();                    // start buffer capture
    var_dump( $object );           // dump the values
    $contents = ob_get_contents(); // put the buffer into a variable
    ob_end_clean();                // end capture
    error_log( $contents );        // log contents of the result of var_dump( $object )
}

Is there another way?

Edit:

Also the woocommerce_cart_item_removed hook is no good as it only runs when user clicks remove button, not when quantity for that item is changed to 0.

And the woocommerce_cart_item_removed hook occurs in a similar way to the woocommerce_cart_item_removed hook.

There is a seperate hook for this occurrence:

woocommerce_before_cart_item_quantity_zero

function woocommerce_before_cart_item_quantity_zero_vvvrbg45kt45($cart_item_key) {
    global $woocommerce;
    $cart_item = $woocommerce->cart->get_cart_item( $cart_item_key );
    $product_id = $cart_item['product_id'];
}
add_action( 'woocommerce_before_cart_item_quantity_zero', 'woocommerce_before_cart_item_quantity_zero_vvvrbg45kt45' );

Try this hook,

add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
function after_remove_product_from_cart($removed_cart_item_key, $instance) {
    $line_item = $instance->removed_cart_contents[ $removed_cart_item_key ];
    $product_id = $line_item[ 'product_id' ];
}

this will fire when an item is removed from the cart. For more information,

Hope this will helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM