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 a functionality where 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 be a 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; }
My name is Benson Karue, I was Software Engineer Telenet Co LTD before I quit and joined YouTube and Udemy as an instructor. I have real-world software experience and have a Bachelor’s degree in Mathematics and Computer Science with 6+ years of coding experience and winning several competitions and coding challenges.
I am familiar with C, C++, C#, Objective-C, Java, Swift, Android, iOS, Windows mobile, J2ME, Blackberry, HTML5, CSS3, JavaScript, Jquery, AJAX, Node.js, PHP, Ruby, Python, Scala, Groovy, PhoneGap JSP, JSF, EJB, Struts, Hibernate and Spring MVC, Flat Design along with databases such as MySQL, Oracle, SQLServer, SQLite and many other technologies and frameworks