Allow only One Product in Cart at a Time in WooCommerce

Allow Only one product purchase at a time in WooCoomerce.

Are you creating a subscription system where you have various plans that users can select in woocommerce? Or anything that you would like to allow only one item in the cart at a time?

This article is going to demonstrate how to allow only one item in the cart at a time in WooCommerce. Basically what we want to achieve with this guide is functionality that when users click on Add to Cart button it’ll clear their previous cart data completely and add new cart data, only allow one product purchase at a time in WooCommerce.

We can even go further to validate if indeed the user bought the product or if the subscription is available. This is will guide for another day, for now let’s see how to allow only one purchase in woocommerce.

How to Allow Only One Purchase in WooCoomerce

The following c0de will allow only one purchase at a time in WooCommerce.

add_filter( 'woocommerce_add_to_cart_validation', 'hacksmile_only_one_in_cart', 99, 2 );
   
function hacksmile_only_one_in_cart( $passed, $added_product_id ) {
   wc_empty_cart();
   return $passed;
}

 



AdBlock Detected!
This site depends on revenue from ad impressions to survive. If you find this site valuable, please consider disabling your ad blocker or pausing adblock for this website.
×