Search
Close this search box.

How to Allow Only One Product in the Cart in WooCommerce

Ever wanted to ensure your customers can only buy one product at a time? It could be for special offers, unique items, or exclusive services.

WooCommerce doesn’t offer this feature by default, but with a simple snippet, you can easily limit the cart to only one product.

In this tutorial, I’ll show you how to do it easily.

Code Snippets to Limit Cart to One Product

Even if you want to limit the cart to just one product, there are two scenarios to consider:

  1. Allow only one product with exactly one quantity.
  2. Allow only one product but with multiple quantities.

I don’t know which one you need. So, I’ll provide code snippets for both cases.

Code: Allow One Product with Max One Quantity

This code ensures the customer can only add one product with one quantity to their cart. If a customer tries to add more, they will see an error message.

You can change the message from this line:

wc_add_notice( 'You can only add one product with one quantity to your cart.', 'error' );

Add the following code to your child theme’s functions.php file

/*
 * Snippet: Allow Only One Product and One Quantity in WooCommerce Cart
 * Author: Toufique Alahi
 * URL: https://wpblogr.com
 * Tested with WooCommerce 9.3.3
 */

add_filter( 'woocommerce_add_to_cart_validation', 'wpblogr_limit_cart_to_one_product_one_quantity', 10, 2 );

function wpblogr_limit_cart_to_one_product_one_quantity( $passed, $added_product_id ) {
    $cart_count = WC()->cart->get_cart_contents_count();
    
    // Check if the cart is empty
    if ( $cart_count == 0 && isset( $_POST['quantity'] ) && $_POST['quantity'] > 1 ) {
        wc_add_notice( 'You can only add one product with one quantity to your cart.', 'error' );
        $passed = false;
    }
    
    // Allow adding the same product with one quantity later
    if ( $cart_count >= 1 ) {
        wc_add_notice( 'You can only add one product with one quantity to your cart.', 'error' );
        $passed = false;
    }
    
    return $passed;
}
one product one quantity

Code: Allow One Product with Multiple Quantities

This code allows customers to add multiple quantities of a single product. However, it blocks them from adding any different products to the cart.

If a customer tries to add a second product, an error message will appear.

wc_add_notice( 'You can only have one product in your cart.', 'error' );

Add the following code to your child theme’s functions.php file

/*
 * Snippet: Allow Multiple Quantities but Only One Product in WooCommerce Cart
 * Author: Toufique Alahi
 * URL: https://wpblogr.com
 * Tested with WooCommerce 9.3.3
 */

add_filter( 'woocommerce_add_to_cart_validation', 'wpblogr_limit_cart_to_one_product', 10, 2 );

function wpblogr_limit_cart_to_one_product( $passed, $added_product_id ) {
    $cart = WC()->cart->get_cart();
    
    if ( !empty( $cart ) ) {
        // Check if the added product is different from the current one in the cart
        foreach ( $cart as $cart_item ) {
            if ( $cart_item['product_id'] != $added_product_id ) {
                wc_add_notice( 'You can only have one product in your cart.', 'error' );
                $passed = false;
                break;
            }
        }
    }
    
    return $passed;
}
one product

How to Add the Code Snippet

You should add the code snippet to the functions.php file in your child theme. If you don’t have a child theme, read this guide on How to Create a Child Theme.

Once you have the child theme, follow these steps:

  • Go to your WordPress dashboard.
  • Hover over Appearance and click on Theme File Editor.
screenshot 1 01
  • On the right side, find functions.php and style.css
  • Click on file names to add the code.
theme file editor

Paste the code and click on Update File.

The code will function as described. However, if the “Add to Cart” button uses AJAX to add products to the cart, the error message will not be displayed.

Despite this, the code will still prevent the addition of multiple products to the cart.

Cheers!

Share

Twitter
Facebook
LinkedIn
Reddit
Pinterest

Related Posts

Toufique Alahi

Hi there! I enjoy writing about WordPress and sharing my hands-on experiences. I explore new features, tips, and tweaks. Stick around for helpful tutorials and insights!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.