WooCommerce: Customize The “You cannot add another __ to your cart” Notification

If a WooCommerce product is set to “sold individually” (i.e. the “Limit purchases to 1 item per order” checkbox is checked in the edit product page) and is already in the cart, the “You cannot add another product to your cart.” notification will appear if you try to add it to cart again, which is fair.

This message appears by default in WooCommerce. There is a workaround to disable it completely, but you can also customize the wording (which is what we’re covering next).

In fact, this notification comes with a PHP filter, which means we can override the content and adapt it to our user base and/or brand tone. Enjoy!

This is the message that appears when you try to add the same product to cart more than once, in case the product is set to “sold individually”

PHP Snippet: Edit The “You cannot add another product to your cart” WooCommerce Error Message

/**
 * @snippet       Custom "You cannot add another __" Woo Message
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_cart_product_cannot_add_another_message', 'bbloomer_override_cannot_add_another_message', 9999, 2 );

function bbloomer_override_cannot_add_another_message( $message, $product_data ) {
    $message = sprintf( 'Sorry, you can only buy one "%s" at a time', $product_data->get_name() );
    return $message;
}

This code effectively customizes the “cannot add another” message in WooCommerce to provide a more user-friendly alternative to the default message. It achieves this by:

  1. Hooking into the appropriate WooCommerce filter.
  2. Intercepting the original message.
  3. Formatting a new message using the product name for clarity.
  4. Returning the modified message for WooCommerce to display.

Explanation:

  • add_filter: This WordPress function attaches a custom function to a specific filter hook.
  • 'woocommerce_cart_product_cannot_add_another_message': This is the filter hook being used, which WooCommerce triggers when it wants to display the “cannot add another” message.
  • 'bbloomer_override_cannot_add_another_message': This is the name of the custom function that will be called to modify the message.
  • function bbloomer_override_cannot_add_another_message( $message, $product_data ): Defines the custom function that modifies the message.
    • $message: The original message WooCommerce wants to display.
    • $product_data: An object containing information about the product the customer is trying to add.
  • $message = sprintf( 'Sorry, you can only buy one "%s" at a time', $product_data->get_name() );:
    • Creates a new message using sprintf to format the text and insert the product name.
    • Uses $product_data->get_name() to retrieve the product name dynamically.
  • return $message;: Returns the modified message, which WooCommerce will then display to the customer.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Customize The “You cannot add another __ to your cart” Notification

  1. Strangely, for your excellent snippets, this is not working.

    Could it be because this is a Variable Product?

    1. Uhm, makes no sense to me, should work for all product types as the message is the same for all

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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