How to Limit Customer Buy One Product Only with WooCommerce

How to Limit Buying One Item Only in WooCommerce
Share this:

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.

 

3 Comments

  1. 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

Leave a Comment.