WooCommerce – Allow only one product in Cart

This snippet can be used for many ways. For instance, your store only allows to buy one product at a time such as “Buy Now”.

Open functions.php under your activated theme and put the code below:

// Only one product in cart
add_filter( 'woocommerce_add_cart_item_data', 'only_one_product_in_cart' );
function only_one_product_in_cart( $cart_item_data ) {
	global $woocommerce;
	$woocommerce->cart->empty_cart();

	return $cart_item_data;
}

Now you can test by adding new product to your existing cart with items, see whether it will added the latest product only and remove all previous products in the cart.

Views (338)

Share on Facebook0Share on Google+0Share on LinkedIn0Tweet about this on TwitterEmail this to someonePrint this page
[Total: 1    Average: 5/5]

Leave a Reply

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

three × two =