Remove the stock quantity from WooCommerce cart error messages Remove the stock quantity from WooCommerce cart error messages wordpress wordpress

Remove the stock quantity from WooCommerce cart error messages


This messages displayed are located in WC_Cart class source code at line 493 and 522 …

What you can try is to replace the text by your custom text version in this function hooked in WordPress gettex filter hook:

add_filter( 'gettext', 'wc_replacing_cart_stock_notices_texts', 50, 3 );function wc_replacing_cart_stock_notices_texts( $replacement_text, $source_text, $domain ) {    // Here the sub string to search    $substring_to_search = 'Sorry, we do not have enough';    // The text message replacement    if( strpos( $source_text, $substring_to_search ) ) {        // define here your replacement text        $replacement_text = __( 'Sorry, we do not have enough products in stock to fulfill your order…', $domain );    }    return $replacement_text;}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This should works (unstested)


Thank you @LoicTheAztec for your reply, but I actually found a filter for this after all, woocommerce_add_error

So my final filter (in functions.php) is this:

function remove_stock_info_error($error){    global $woocommerce;    foreach ($woocommerce->cart->cart_contents as $item) {        $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];        $product = new \WC_Product_Factory();        $product = $product->get_product($product_id);        if ($item['quantity'] > $product->get_stock_quantity()){            $name = $product->get_name();            $error = 'Sorry, we do not have enough "'.$name.'" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.';            return $error;        }    }}add_filter( 'woocommerce_add_error', 'remove_stock_info_error' );

This should resolve it across the board.

NOTE! I also found that the input boxes have a max attribut, which in turn means that anyone can still see the actual total available amount (by either simply using the built in increment (which will stop when reaching max value) or just enter to high of a value, click update cart and you will get a notice that the amount has to be equal to or less than X (max value)).

To combat this I added a simple JS in my pre-existing "woo-xtra.js":

var qty             = $('form.woocommerce-cart-form').find('input.qty');// Reset max value for quantity input box to hide real stockqty.attr('max', '');

This way there is no max value, but the user will still get the error (if over the limit) from above :)

I.e. Problem solved