Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 909
MariaZ Avatar asked Jan 07 '23 23:01

MariaZ


1 Answers

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 );
like image 61
helgatheviking Avatar answered Jan 16 '23 00:01

helgatheviking