How to remove cross sell / up sells products along with main product from cart in woocommerce

How to remove cross sell / up sells products along with main product from cart in woocommerce

August 26, 2018

Step 1: Finding the right action hook fired by WooCommerce

First thing, we need to find the action hook fired by WooCommerce when removing the product from cart.
Woocommerce provides “woocommerce_cart_item_removed” action hook with two arguments; $cart_item_key and $cart_object.

Important thing here to note is that, WooCommerce doesn’t provide the product ID of removed cart item . Only the cart item key and cart object.

We can’t decode hashed md5 encoded cart item key to retrieve product ID. We can’t get the product ID of removed item from $cart_object as well because this $cart_object will be the cart objects after the removal of product from cart.

How to get the product ID of removed cart item?

Note: Woocommerce generates unique md5 hashed for each product and uses that key to add / remove product to cart.


//Generate cart item key
cart_item_key = WC()->cart->generate_cart_id( $product_id );
//Add product to cart
WC()->cart->remove_cart_item( $cart_item_key ); 

Step 2: Save mapping of product item key and product ID in session

Now, we can save the mapping of product ID and product item key in woo commerce session for future reference to get the product ID by product item key by using the “woocommerce_add_to_cart” action hook.


add_action( 'woocommerce_add_to_cart', function( $cart_item_key, $product_id){

    $product_id_cart_item_pairs = WC()->session->get( 'product_id_cart_item_pairs' , array() );

    $product_id_cart_item_pairs[$cart_item_key] = $product_id; 

    WC()->session->set( 'product_id_cart_item_pairs' , $product_id_cart_item_pairs );

}, 99, 2);

Note: Woo Commerce session will work only after the initialization of cart session.

Step 3: Remove the cross-sell / upsell products from cart

In final step, we will get the product ID of removed cart item, find it’s cross-sell / up-sell products and remove them as well from cart.


add_action( 'woocommerce_cart_item_removed', function( $cart_item, $cart){
	$product_id_cart_item_pairs = WC()->session->get( 'product_id_cart_item_pairs' , array() ); 
	// Get the product ID using session store 
	$product_id = isset($product_id_cart_item_pairs[$cart_item]) ? $product_id_cart_item_pairs[$cart_item] : false;
	if( !$product_id ){
		return;
	}
	
	$product = wc_get_product( $product_id ); 
	
	//get product cross sell products 
	$cross_sell_products = $product->get_cross_sell_ids(); 
	//var_dump($cross_sell_products);
	if(!empty($cross_sell_products)){
		foreach($cross_sell_products as $cross_sell_product_id){
			//Get cart item Key
			$cross_sell_product_cart_item_key = WC()->cart->generate_cart_id( $cross_sell_product_id );
			if(  WC()->cart->find_product_in_cart( $cross_sell_product_cart_item_key ) ){
				 WC()->cart->remove_cart_item( $cross_sell_product_cart_item_key ); 
			}
		}
	}

	//get product up-sell products 
	$upsell_sell_products = $product->get_upsell_ids(); 
	if(!empty($upsell_sell_products)){
		foreach($upsell_sell_products as $up_sell_product_id){
			//Get cart item Key
			$up_sell_product_cart_item_key = WC()->cart->generate_cart_id( $up_sell_product_id );
			if(  WC()->cart->find_product_in_cart( $up_sell_product_cart_item_key ) ){
				 WC()->cart->remove_cart_item( $up_sell_product_cart_item_key ); 
			}
		}
	}
}, 99, 2 );

Original Post: How to remove cross sell / up sells products along with main product from cart