Adding a hook to Remove item in a woocommerce cart

2019-06-03 05:45发布

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. 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.

Thanks so much!

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-03 06:49

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_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. 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 );
查看更多
登录 后发表回答