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.

Enjoy!

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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

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

2 thoughts on “WooCommerce: Remove Second Item When Item Removed From Cart

  1. Hi Rodolfo,
    I wanted to share this. I needed to remove all products of a specific category IF the user removed a product from a different category which is required to purchase the other products. (Only allow user to purchase product from Category A if they have already purchased a product from Category B.)
    I have that code in another place, but we needed to REMOVE any “unauthorized” products from the cart if they remove a product from the required category.
    Hope this helps someone.

    add_action('woocommerce_remove_cart_item', 'madcow_remove_product_from_cart', 10, 2);
    function madcow_remove_product_from_cart($item_key, $cart) {
    
        //Get the ID of the product removed
        $removed_id = $cart->cart_contents[$item_key]['product_id'];
        //If the Product is a standard beef share or beef box, remove individual cuts from the cart
        if (has_term(array('standard-beef-share', 'boxed-beef'), 'product_cat', $removed_id)) {
    
            foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
                $product = $cart_item['data'];
                if (has_term('individual-cuts', 'product_cat', $product->get_id())) {
                    $cart->remove_cart_item($cart_item_key);
                }
            }
        }
    }
    
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 *