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.

 

25 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

  2. 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?

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

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

  5. 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;
    }

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

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

  8. 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?

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

Leave a Comment.