Which Hook to alter quantity update in WooCommerce cart page?

10,694

Solution 1

You should use woocommerce_after_cart_item_quantity_update action hook that has 4 arguments. But when quantity is changed to zero, woocommerce_before_cart_item_quantity_zero action hook need to be used instead (and has 2 arguments).

Below is a working example that will limit the updated quantity to a certain amount and will display a custom notice:

add_action( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
function limit_cart_item_quantity( $cart_item_key, $quantity, $old_quantity, $cart ){
    if( ! is_cart() ) return; // Only on cart page

    // Here the quantity limit
    $limit = 5;

    if( $quantity > $limit ){
        // Change the quantity to the limit allowed
        $cart->cart_contents[ $cart_item_key ]['quantity'] = $limit;
        // Add a custom notice
        wc_add_notice( __('Quantity limit reached for this item'), 'notice' );
    }
}

This code goes on function.php file of your active child theme (or theme). Tested and works.

As this hook is located in WC_Cart set_quantity() method, is not possible to use that method inside the hook, as it will throw an error.


To trigger some action when quantity is set to Zero use:

add_action( 'woocommerce_before_cart_item_quantity_zero', 'action_before_cart_item_quantity_zero', 20, 4 );
function action_before_cart_item_quantity_zero( $cart_item_key, $cart ){
    // Your code goes here
}

Solution 2

Maybe this hook? do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity );

http://hookr.io/actions/woocommerce_after_cart_item_quantity_update/

Share:
10,694
Bombo
Author by

Bombo

Updated on June 12, 2022

Comments

  • Bombo
    Bombo almost 2 years

    I'm trying to fire a function when the quantity of a product is changed in cart. More specifically I want to run this function when a customer modify the amount in a cart.

    I'm looking to find the amount left in a cart then to intercept the update cart event

    Currently I'm using:

    add_action( 'woocommerce_remove_cart_item', 'my function');
    

    When I press "update_cart", it doesn't seem to work. Any advice? Thank you!

  • Bombo
    Bombo about 6 years
    This hook it's run thank you so much. I created file txt that returns the quantity left for single product(when i change it). But if change more quantity in a cart doesn't work.
  • Bombo
    Bombo about 6 years
    Yes, you're right. I'm trying to get all the quantities changed in the cart, and write it's into file txt.
  • LoicTheAztec
    LoicTheAztec about 6 years
    @Bombo You maybe can set that in a custom WC_Session variable using WC()->session->get() and WC()->session->set() that will keep all item changing quantities history… Then, once order is submitted you can save this data in order meta data. So this data will be accessible.
  • Bombo
    Bombo about 6 years
    Ok, another question, why when i change the quantity on the single item at 0, it's doesn't print in my file txt? Sorry for my question but i need help!
  • LoicTheAztec
    LoicTheAztec about 6 years
    @Bombo If you change the quantity to Zero it doesn't use that hook but instead it uses hook woocommerce_before_cart_item_quantity_zero, because in this case item is removed from cart… see it in the source code of WC_Cart set_quantity().
  • Bombo
    Bombo about 6 years
    thank you very much you are my hero !! Now remains only problem, that when i modify one quantity for a single item (also 0) it's run but if i modify two or more quantity for items doesn't run.
  • LoicTheAztec
    LoicTheAztec about 6 years
    @Bombo Maybe you should ask a new question around this, including in the question all the related code you are using, with some details about what is working what not and what you expect to have. Once done notify me here.
  • Bombo
    Bombo about 6 years
    thanks for everything, I try alone, in case i ask a new question.
  • Ido Cohen
    Ido Cohen over 2 years
    For some reason is_cart() return false when I use this hook.. is it a bug?