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.
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With