I have started using WooCommerce since year 2012 and i like it because it provides me many customized options to suit my needs or client requirement.
Case Study:One of the possible situation is where i need to disable the shopping cart. In this way, the customer only can buy one item, if the customer goes to another product and tries to buy it, the cart will be cleaned and the last item added.
Solution:
Open your theme functions.php, and put below code at the end.
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' ); function woo_custom_add_to_cart( $cart_item_data ) { global $woocommerce; $woocommerce->cart->empty_cart(); return $cart_item_data; }
Finally, save the file. And 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.
OMG I LOVE YOU MAN. THIS IS THE FUNCTION I NEEDED SO BAD.
Hi John, thanks for your love, haha ๐ Glad to know this code help people around, cheers!
Hello terry,
I need the same thing but only for a specific product.
For example i have n products A,B,C …W
You can buy A to W in all the quantitรฉ you want and you can mixte them but the product Z can only be sale alone.
I don”t find a solution.
Do you think you could help me ?
I am not very kind with code.
Many thanks
Regards
JM
THX!!!!!!! VERY THX!!!!! YOU ARE BEST!!!!!
You Sir are a life saver… you saved me hours of Search+Work…
Thanks a lot.
all I could do for you is +1 on google and like on Facebook.
Thanks again.
Glad it helped, Khan. Cheers!
Thanks Hammad, thanks for your support.
A very huge thank you, I battled with this issue for hours but your function aced it in seconds! Thanks man.
Hi Sylvester, glad to know it solved your problem instantly ๐
Hi! I tried your code but surprisingly it didnt work. In the site I’m working on, the product is being added thru an A link using the url param “?add-to-cart=999”. May that be the cause? Looks like the filter you posted here cannot intercept the mentioned url param.
Any ideas?
Thanks a lot for the code. Its works just right.
Yeah, good to know it helped ๐
Sent the whole site into blank pages….Don’t think this works anymore!
Hey Terry, I’m just wondering if you could respond this question:
I need the same thing but only for a specific product.
For example i have n products A,B,C โฆW
You can buy A to W in all the quantitรฉ you want and you can mixte them but the product Z can only be sale alone.
Best,
Jorge
Terry,
Thanks for the code. Just what I was looking for!
I also use your direct checkout. Really sweet plugin.
Thanks for all your hard work helping us all out.
Terrytsang,
Thanks for this beautiful solution just was looking for
thanks so much you are great….
Thanks, this was perfect!
Jorge,
Not sure if you still need to limit by product.
Below is pseudocode.
HTH.
add_filter( ‘woocommerce_add_to_cart_validation’, ‘check_if_cart_has_product’, 10, 3 );
function check_if_cart_has_product( $valid, $product_id, $quantity ) {
$limit_product_id = 9999; //product Z id
if ($limit_product_id == $product_id) {
$cart = WC()->cart->get_cart();
if (!empty($cart) && $valid){
foreach ($cart as $cart_item_key => $values) {
$_product = $values[‘data’];
if ( $product_id == $_product->id ) {
wc_add_notice( ‘Sorry. Only one item is allowed.
If you would like to add another, please remove the item in the cart.’, ‘error’ );
return false;
}
}
}
}
return $valid;
}
hey nice article
i want that customer can purchase only product from woo commerce in life time. Next time when visit shop page then automatically redirect to my account page
thank you!
Hi terrytsang.
i have the same problem than johnM
I need to sell one “Y” product in only one order.
I want prevent my customer from bying another product with the “Y” product.
Do you have an idea ?
Thank’s a lot !
Hi
Is it possible to change this code this way:
Same happens as in your’s solution but not for the whole cart just for a specific category.
So only one product is allowed to buy from a specific category?
Pingback: [์๋ํ๋ ์ค] ์ฐ์ปค๋จธ์ค์์ ํ๋์ ์ํ๋ง ๊ตฌ์ ๊ฐ๋ฅํ๋๋ก ์ค์ ํ๊ธฐ - ์๋ํ๋ ์ค ์ ๋ณด ๊พธ๋ฌ๋ฏธ
Nice Code Man….
Can we make it for category specific. e.g. the user can buy from one category at a time and if he navigate to another category the cart goes empty and new category products get added like now.
Works well. Thank you so much…
Hi terrytsang
I want to restrict customer to buy from multiple vendors at a time.
Could you please help me out.
Sincerely Sameer
Used this code 2 years later on WooCommerce 2.6.4 and still works like a champ. Thanks for putting it out there for us.
Thankkkkkkks
Hello, it’s a great article but I have a question: how can I limit one product by brand in the cart, using your logic this ?? I would like to do this?
thank you
Kiko – direct from Brazil
Thank you very much for this! It worked perfectly! How can the code be modified to take the user to the cart instead of reloading the product page?
My goal is to have the user taken to the cart immediately.
Thanks it works. Wow i needed this.
thanks of lot
Thank you SO MUCH for this!!!
Great information dude, thanks, it’s helping me
Terima kasih. Luar biasa!
Thanks. Awesome!
Awesome!!
This piece of code is still working in 2018!
Thank you for sharing. You made my day!
Thanks, are working
But i need the changes error message, so how can i changes the error message
really very helpful and saved time.
Thanks again.
Thank you so much TERRYTSANG for such great help. Appreciate your efforts
I need to sell one โYโ product in only one order.
I want to prevent my customers from buying another product with the โYโ product.
Any solution?
2020 and still working. Thumbs up. This saved my project.
Hi Henry, I’m glad it helps.
Thanks so much, this was amazingly helpful, clean and easy!
It’s my pleasure, I’m glad it helps.