0
votes

Hy guys. I have three product in my cart. Let's try to imagine that the image is the cart and i have modify the quantity of product and then press update cart.

enter image description here

When i press update cart, my function runs, than prints all my item after modify, into ITEMS.json. There are all my items and with the quantity modify of the Shoes, but into my file there is also Hoodie with quantity = 3. How is it possible that i delete it? it and i print items after update cart.

add_action('woocommerce_after_cart_item_quantity_update','my_function');

function my_function()
{

    global $woocommerce;    
    $items = $woocommerce->cart->get_cart();
    file_put_contents('ITEMS.json', print_r($items, true));
}

Is there something to distinguish the products delete from product modify?

Is there a hook which returns the list of products deleted ?

1

1 Answers

1
votes

According to the documentation, you can retrieve all needed information from the parameters of the hooked function:

function my_function( $cart_item_key, $quantity, $old_quantity ) {
    global $woocommerce;  
    $items = $woocommerce->cart->get_cart();

    // Check that quantity is not equal to 0 and that the item exists in the cart
    if($quantity && isset($items[$cart_item_key])) {
        $content = '';
        // TODO: Here you must fill $content with needed information from $items[$cart_item_key]
        // Below, I add the new quatity to the $content string.
        $content .= $quantity;
        // Write in JSON file
        file_put_contents('ITEMS.json', $content);
    }
};

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