Allow customer to purchase only one item - WooCommerce

In one of my recent WooCommerce project, I want to allow user to purchase only one item ( any one, but only one product ) from my shop!

To achive this functionalitty, we want to add a small code snippet in our theme’s / child theme’s functions.php file.

add_filter( 'woocommerce_add_cart_item_data', 'woo_allow_one_item_only' );

function woo_allow_one_item_only( $cart_item_data ) {

	global $woocommerce;
	$woocommerce->cart->empty_cart();

	// Do nothing with the data and return
	return $cart_item_data;
	
}

The above code snippet is a filter function that will work on woocommerce_add_cart_item_data filter hook. When we add a new product in to cart, our function become triggered and it will empty current cart items.

Need Help?

Do you need any help on WordPress & WooCommerce? I'm happy to help you.

Contact Me

Recent Posts

  1. Move files recursively from source to remote server - SCP Command
  2. Brief about Magento 2 cache commands
  3. Magento 2 customer account password options
  4. Enable or disable maintance mode - Magento 2
  5. Replace string in multiple files - Command line


Questions / Comments

If you found this article interesting, found errors, or just want to discuss about them, please get in touch.