returns 产品从购物车中删除的 WooCommerce 挂钩

WooCommerce hook that returns products delete from cart

伙计们。 我的购物车中有三种产品。让我们试着想象图像是购物车,我已经修改了产品数量,然后按更新购物车。

当我按下更新购物车时,我的功能运行,然后在修改后将我的所有项目打印到 ITEMS.json。有我所有的物品和鞋子的数量修改,但在我的文件中还有数量 = 3 的连帽衫。我怎么可能删除它?它和我在更新购物车后打印项目。

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));
}

有什么东西可以区分产品删除和产品修改吗?

是否有挂钩returns删除产品列表?

根据文档,您可以从挂钩函数的参数中检索所有需要的信息:

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 );