Remove stock quantity from error messages in WooCommerce basket

Iin WooCommerce, I set woocommerce-> settings-> products-> inventory-> format for displaying stocks on "Never show the remaining quantity in stock . "

However, if the customer advertises the product in the basket, he continues to move the page or check page and enters a value that is higher than in our warehouse, this error message:

Sorry, we don’t have enough " {product_name}" in stock to complete your order ( {available_stock_amount}in stock). Change your cart and try again. We apologize for any inconvenience this may cause.

What filter can I use to edit this output? I do not want him to show (absolutely anywhere in the store) the real available amount of stock.

I 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, I want to filter out the " (%2$s in stock)" part . But I can not find a filter for this.

+4
source share
2 answers

Thanks @LoicTheAztec for your answer, but actually I found a filter for this, woocommerce_add_error

So my last filter (in the .php function) 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' );

.

! , max, , , , ( ( ) , , , X ( )).

, JS "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', '');

, , ( ) :)

.. .

+2

WC_Cart 493 522...

- , WordPress gettex.

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;
}

function.php ( ), .

()...

+1

Source: https://habr.com/ru/post/1682212/


All Articles