Allow Only One Specific Product in the WooCommerce Cart at a Time

Allow only one copy of a product in the WooCommerce cart

Sometimes a customer must be able to add a product to the cart only once, and a second click on its “Add to cart” button must remove it from the cart, like a toggle. For example, an option or a service that cannot be bought in quantity.

Problem: A product can be in the WooCommerce cart only once. If the customer adds it again, it has to be removed from the cart.

Solution: In the end the task comes down to one question: is the product already in the cart? There are two ways to check it.

Way 1. Loop through the cart

<?php
$product_id = 555;
$found      = false;

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    if ( (int) $cart_item['product_id'] === $product_id ) {
        WC()->cart->remove_cart_item( $cart_item_key );
        $found = true;
        break;
    }
}

if ( ! $found ) {
    WC()->cart->add_to_cart( $product_id, 1 );
}

Note the (int): product IDs in the cart may be strings, and with a strict comparison '555' === 555 is false.

Way 2. The cart item hash

First a few words about the WooCommerce cart. Every item in it has a hash key, for example 53fde96fcc4b4ce72d7739202324cd49. You can see it on the cart page, in the name of the quantity field or in the “remove” link. The hash is built from the product ID (and the variation and extra item data), so we can generate it ourselves and look for it in the cart:

<?php
$product_id = 555;

$cart_id = WC()->cart->generate_cart_id( $product_id ); // hash by product ID
$in_cart = WC()->cart->find_product_in_cart( $cart_id ); // the key, or '' if not in the cart

if ( $in_cart ) {
    WC()->cart->remove_cart_item( $in_cart );
} else {
    WC()->cart->add_to_cart( $product_id, 1 );
}

This way is shorter and does not loop through the cart. For a variable product pass the same variation ID and attributes to generate_cart_id() as to add_to_cart(), otherwise the hash will be different.

Using it with the normal “Add to cart” button

The code above fits a custom AJAX handler. With the standard WooCommerce button, the same check goes into the add-to-cart validation: if the product is already in the cart, remove it and stop the second add.

<?php
add_filter( 'woocommerce_add_to_cart_validation', 'toggle_single_product_in_cart', 10, 2 );

function toggle_single_product_in_cart( $passed, $product_id ) {
    if ( 555 !== (int) $product_id ) {
        return $passed;
    }

    $in_cart = WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product_id ) );
    if ( $in_cart ) {
        WC()->cart->remove_cart_item( $in_cart );
        wc_add_notice( __( 'The product has been removed from your cart.', 'theme-name' ), 'notice' );
        return false;
    }

    return $passed;
}

NOTE: If the product must simply never have a quantity above 1, it is easier to tick “Sold individually” on the Inventory tab of the product. Then WooCommerce does not allow a second copy in the cart at all.