Categories
Woocommerce

Remove old items from cart when new items are added in woocommerce

Lets say you want users to click a buy now button and it should add a particular product with certain quantity to the cart by removing all other items. That’s when you will need this code handy. Just paste it in your functions.php file

// remove old items in cart when new items added
	add_filter('woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 1, 3);
	function remove_cart_item_before_add_to_cart($passed, $product_id, $quantity) {
		if (!WC()->cart->is_empty()) {
			WC()->cart->empty_cart();
		}
		
		return $passed;
	}

By Murali Kumar

I am a Full Stack Web Developer with over 12 years of experience. I love WordPress & WooCommerce.

If you need help of a WordPress developer for your website, Get in touch now!.

Leave a Reply

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