How to find out product id from “woocommerce_cart_item_removed” hook?

I have code

add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart' );
function after_remove_product_from_cart($removed_cart_item_key, $instance) {
    $product_id = $removed_cart_item_key['product_id'];
}

I want to find out a way to get product id or actual product object itself using $removed_cart_item_key. How do you do it? I cannot find any references, thanks.

Related posts

3 comments

  1. should be something like this…

    add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
    function after_remove_product_from_cart($removed_cart_item_key, $cart) {
        $line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
        $product_id = $line_item[ 'product_id' ];
    }
    
  2. Because it happen before the cart item is removed, you need to use woocommerce_remove_cart_item instead of woocommerce_cart_item_removed, to retrieve this product item.

    add_action( 'woocommerce_remove_cart_item', 'after_remove_product_from_cart', 10, 2 );
    function after_remove_product_from_cart($removed_cart_item_key, $cart) {
        $product_id = $cart->cart_contents[ $removed_cart_item_key ]['product_id'];
    }
    

    See this source from helgatheviking

  3. You can use this method to fetch the product id of item removed from cart, It worked for me

    $product_id = $cart->cart_contents[$cart_item_key][‘product_id’];

Comments are closed.