How to only allow one product in cart, WooCommerce tips and tricks
Sometimes we need to allow only one product to one order, it needs to do that when you are selling something, where all products can’t be shipped by box or email. Like if you are reselling some streaming subscriptions or if you are selling some software or if you are selling some digital product, as some service reselling is connected with API and you have to limit your store order system to one order with only one product, To do that you can limit your product in the cart in WooCommerce. like if someone adds one product to the cart and adds another product, the previous product will be removed and the new product will be added to the cart with the notice “you can only add one product”.
We can do it by adding one PHP snippet, where we will hook in the cart, check if any previous item is there or not, if there is an item we will remove the cart item and add a new one, and show notice to the user that user can’t add more than one product per order. Check out the PHP snippets below :
add_filter( 'woocommerce_add_to_cart_validation', 'the_daily_wp_only_one_product_in_cart', 9999, 2 ); function the_daily_wp_only_one_product_in_cart( $passed, $added_product_id ) { wc_empty_cart(); return $passed; }
Step Two: Add it to your theme Function.php
if you are not a Developer, Please don’t try to do it yourself. To add this code to your website, you have to go to your domain file manager, and you have to go to wp-content -> themes -> your active theme -> functions.php file. open it and scroll the bottom, find PHP closing tag “?>” and copy the above code. paste it before php closing tag. and save it. that’s it. now try to add more than one product, you will see the magic.
Conclusion
This is the easiest way to limit your customer cart to one product. if you still can’t limit your customer please let us know in the comment below. here is more WooCommerce tutorial you would like to check out. Please follow our facebook page and please subscribe to our youtube channel
One Comment