Search code examples
phpwordpresswoocommercecartstock

Remove the stock quantity from WooCommerce cart error messages


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.


Solution

  • 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