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

Written by

in

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

// 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;
	}

Comments

5 responses to “Remove old items from cart when new items are added in woocommerce”

  1. irfanelahi

    i have a issue
    i have on item in 2 different variation if i select all two variation in cart ,my cart could not be empty otherwise old item clear how can i fix?

  2. ahmed

    thanx it work for me

  3. elmohajir

    Thank you its works very Good

  4. Prashant

    Thanks it Works

  5. Gareth

    This actually worked. As I wanted it. Needed for the old product in the cart to be removed if the new was added and this fixed it. Thanks so much

Leave a Reply

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