WooCommerce: Remove Product From Cart Programmatically

We already saw how to add a product to cart automatically, for example if you visit a specific page or if there are no products in the cart – but today we want to find out how to do the opposite: if a certain condition is met, we want to remove a product ID from the cart.

This becomes a little complex – while adding an item to cart requires just its product ID, removing it from the cart forces you to know the “cart item key”. Japanese, I know, but just copy the snippet and you’re done!

How to automatically remove a product from the Cart

PHP Snippet: Remove Item from Cart Automatically

In the example below, I’m targeting product ID = 282 and checking what the current cart total is. If I go over the $50 threshold, the snippet loops through the cart items and stops if it finds the product. At this stage, it stores its “cart item key” and finally uses remove_cart_item() function to remove it.

/**
 * @snippet       Remove Cart Item Programmatically - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'template_redirect', 'bbloomer_remove_product_from_cart_programmatically' );

function bbloomer_remove_product_from_cart_programmatically() {
	if ( is_admin() ) return;
	$product_id = 282; // product id
	$cart_total = 50; // replace with threshold
	$in_cart = false;
	foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		if ( $cart_item['product_id'] === $product_id ) {
			$in_cart = true;
			$key = $cart_item_key;
			break;
		}
	}
	if( WC()->cart->total > $cart_total ) {
		if ( $in_cart ) WC()->cart->remove_cart_item( $key );
	}
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

2 thoughts on “WooCommerce: Remove Product From Cart Programmatically

  1. Could you let me know how to amend the code such that if a user adds one product, then it will automatically remove all other products that have been previously added to the cart?

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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