简体   繁体   English

添加一个钩子到woocommerce购物车中删除项目

[英]Adding a hook to Remove item in a woocommerce cart

In the woocommerce cart, when the user presses the REMOVE ITEM button on the cart I am trying to retrieve some post meta from an item in the cart. 在woocommerce购物车中,当用户按下购物车上的REMOVE ITEM按钮时,我试图从购物车中的商品中检索一些邮政元素。 Something like: 就像是:

$removed_stock = get_post_meta( $product_id, 'more_info_data', 'x' );

To do that I am adding an action: 要做到这一点,我正在添加一个动作:

function ss_cart_updated( $item_id ) {

    print "<pre>";
    print_r (WC()->cart->get_cart());
    print "</pre>";
    exit;

};

// add the action
add_action( 'woocommerce_cart_item_removed', 'ss_cart_updated' );

Unfortunately this only list all the products in the cart that have been not removed. 不幸的是,这只列出了购物车中未被删除的所有商品。 The item that has not been removed it is not listed anymore. 尚未删除尚未删除的项目。

I tried "woocommerce_get_remove_url" and "woocommerce_cart_item_remove_link" they dont seem to do anything for me. 我试过“woocommerce_get_remove_url”和“woocommerce_cart_item_remove_link”他们似乎没有为我做任何事情。

Thanks so much! 非常感谢!

I think you need to use the woocommerce_remove_cart_item which runs just before the item is actual unset from the cart contents array. 我认为您需要使用woocommerce_remove_cart_item ,它在物品实际未从购物车内容数组中取消之前运行。 woocommerce_cart_item_removed happens after the item is removed, so as you have found out there's no way to get any information about the product. 删除项目会发生woocommerce_cart_item_removed ,因为您发现无法获得有关该产品的任何信息。 Untested, but try this: 未经测试,但试试这个:

function ss_cart_updated( $cart_item_key, $cart ) {

    print "<pre>";
    $product_id = $cart->cart_contents[ $cart_item_key ]['product_id']; 
    print_r(get_post_meta( $product_id, 'more_info_data', true ));
    print "</pre>";
    exit;

};
add_action( 'woocommerce_remove_cart_item', 'ss_cart_updated', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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