WooCommerce: Remove Second Item When Item Removed From Cart

This approach might be relevant for specific promotional offers (e.g. BOGO) or custom coded product bundles. In this case, you may want to remove a second item from the cart – programmatically – once a product is removed.

Of course, a relationship is needed between the two cart items via custom cart item meta data (for example, the “bundled” item will need to specify the ID of the “base” product in the cart within some custom cart item meta, so that we can “listen” to its removal).

Alternatively, you can check if a pair of product IDs are in the cart, and if one of them is removed, we’ll automatically remove the other – this is the case study we’ll go through today as it’s much easier.


Let’s imagine that these two items are “together”, and if one is removed from the cart we also want the other to be removed programmatically!

PHP Snippet: Remove Product ID When Another Product ID Is Removed @ WooCommerce Cart

This code snippet ensures that when a customer removes a product from their WooCommerce cart that’s part of a bundle, the other product in the bundle is automatically removed as well.

The code uses add_action to hook the bbloomer_remove_bundle_from_cart function to the woocommerce_remove_cart_item action. This means whenever a product is removed from the cart, the bbloomer_remove_bundle_from_cart function will be triggered.

bbloomer_remove_bundle_from_cart function takes two arguments, one of which is $removed_cart_item_key which holds the unique key of the product that was just removed.

The function defines an array $bundle_ids containing the IDs of the products considered part of the bundle. It then checks if the removed product’s ID (using $cart_item['product_id']) exists within the $bundle_ids array.

If the removed product is part of a bundle, the function retrieves the ID of the other product in the bundle using array_diff and reset. It then generates a unique cart ID for the remaining product and checks if it’s already present in the cart. If the other product is found, it removes that product from the cart using WC()->cart->remove_cart_item.

The code adds a notice to the user informing them that both products were removed since they are sold together – see screenshot below.

 * @snippet       Programmatically Remove Second Item From WooCommerce Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/

add_action( 'woocommerce_remove_cart_item', 'bbloomer_remove_bundle_from_cart', 9999, 2 );

function bbloomer_remove_bundle_from_cart( $removed_cart_item_key, $cart ) {

   $bundle_ids = array( 123, 456 ); // BUNDLE PRODUCT IDS
   $cart_item = WC()->cart->get_cart_item( $removed_cart_item_key ); // GET PRODUCT REMOVED

   if ( in_array( $cart_item['product_id'], $bundle_ids ) ) {
      $other_product_id = reset( array_diff( $bundle_ids, array( $cart_item['product_id'] ) ) ); // GET ME THE OTHER ID
      $product_cart_id = WC()->cart->generate_cart_id( $other_product_id );
      $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

      if ( $in_cart ) {

         remove_action( 'woocommerce_remove_cart_item', 'bbloomer_remove_bundle_from_cart', 9999, 2 );
         WC()->cart->remove_cart_item( $in_cart );
         wc_add_notice( 'Note: both products have been removed since they are sold together.', 'error');
         add_action( 'woocommerce_remove_cart_item', 'bbloomer_remove_bundle_from_cart', 9999, 2 );



The custom red notification appears once the bundle is removed from the cart!

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: Remaining $$$ to Get Free Shipping Notification @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. So, here’s how we show a simple message on […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *