How to remove cross-sells / up-sells products along with main product from cart

Step 1: Finding the right action hook fired by WooCommerce

To set up the up-sells and cross-sells products in WooCommerce, you can follow the instruction here

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.

do_action( 'woocommerce_cart_item_removed', $cart_item_key, $this );
//$this is the cart object here

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.

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

We can use the cart function generate_cart_id() of WooCommerce to generate the cart item key. The code snippet is mentioned below for reference. 

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

After getting the cart item key, we can use that cart item key to remove that particular particular product item from cart.

//Remove Product item with item key $cart_item_key
WC()->cart->remove_cart_item( $cart_item_key );

The next step is to save the mapping relation of product ID with its corresponding 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-sells / up-sells products from cart

In final step, we will get the product ID of removed cart item, find it’s cross-sells / up-sells 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 
	$upsells_products = $product->get_upsell_ids(); 
	if(!empty($upsells_products)){
		foreach($upsells_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 );

Hope it helps someone at some point.You can find the full code in the following link. 

You can find Full code here

Comments

  1. Pingback:How to remove cross sell / up sells products along with main product from cart in woocommerce – Engineering Sarokar

  2. Rob

    Hi – this is just what I was looking for. The problem I am having is that this works well for Simple products, but I have color variations added to my product – white, black etc. but on variable products it only removes the main product and not the Cross-sell.

    Any idea how to make this work for variable products?

    Thanks

    Rob

  3. Author
    prasidhda

    You are right, the above code only works for simple product. However, the same logic can be used for variation product as well but it is somewhat tricky.
    I will reply in the same comment after implementing for variation product as well.

    Thanks for visiting 🙂

  4. Author
  5. Author

Leave a Reply

Your email address will not be published.