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!
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 );
}
}
}