Search code examples
phpwordpresswoocommerceproductstock

Change add to cart stock error notice in WooCommerce


When a customer tries to add too many of an item to cart, they see this error notice:

You cannot add that amount to the cart - we have X in stock and you already have Y in your cart.

This behavior is included on WC_Cart add_to_cart() method source code at line 1067.

I will like to hide stock details and replace it with:

You cannot add that amount to the cart — we don't have enough in stock.

How to change this add to cart stock error notice in WooCommerce, without overwriting source code?


Solution

  • You can change the said text without modifying the plugin files with the help of "Say What" Plugin. Follow the following steps to change the text:

    1. Install Say What plugin

    2. Visit Tools > Text Changes

    3. Click Add New

    4. Fill the fields as per your need e.g.

      • Original string: You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.
      • Text domain: woocommerce
      • Text context:
      • Replacement string: You cannot add that amount to the cart — we don't have enough in stock.
    5. Click Add button to save the changes.