4

Iin WooCommerce, I've set woocommerce->settings->products->inventory->stock display format to "Never show quantity remaining in stock".

However if a customer ads a product to the cart, continues to cart or checkout page and enter a value higher than we have in stock they get this error message:

Sorry, we do not have enough "{product_name}" in stock to fulfill your order ({available_stock_amount} in stock). Please edit your cart and try again. We apologize for any inconvenience caused.

What filter can I use to edit this output? I don't want it to show (absolutely anywhere on the front end shop) the actual available stock amount.

I've found that this is handled in a function (check_cart_item_stock) in, [root]->wp-content->plugins->woocommerce->includes->class-wc-cart.php on line 491:

if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) {
    /* translators: 1: product name 2: quantity in stock */
    $error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity(), $product ) ) );
    return $error;
}

So what I want to filter out is the "(%2$s in stock)" part. But I can't find any filter for this.

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
axelra82
  • 479
  • 5
  • 21

2 Answers2

2

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)

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
2

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 stock
qty.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

axelra82
  • 479
  • 5
  • 21
  • bit of a longshot now but I have this exact same problem Where the input boxes have a max attribute when they try to order more than we have in stock. This line of JS would help me. You say you added the code to "woo-xtra.js". I dont have this js file, probably because i am running a different theme, where else could I add this? – Chris Jan 13 '21 at 11:38
  • @Chris Sorry, this is a 4 year old question... I don't remember any details about this. However the woo-extra.js file is a custom JS I used for extra (i.e. custom) WooCommerce javascript. At the top of my head I'd assume I included it somewhere in the head. Hope that helps. – axelra82 Mar 26 '21 at 14:52