从 WooCommerce 购物车错误消息中删除库存数量

axe*_*a82 5 php wordpress cart stock woocommerce

在 WooCommerce 中,我已将 woocommerce->settings->products->inventory->stock display format 设置为"Never show amount left in stock"

但是,如果客户将产品广告到购物车,继续购物车或结账页面并输入高于库存的价格,他们会收到此错误消息:

抱歉,我们没有足够的{product_name}库存来履行您的订单({available_stock_amount}库存)。请编辑您的购物车并重试。我们对造成的任何不便表示歉意。

我可以使用什么过滤器来编辑此输出?我不希望它显示(绝对在前端商店的任何地方)实际可用库存量。

我发现这是在第 491 行[root]->wp-content->plugins->woocommerce->includes->class-wc-cart.php中的函数 (check_cart_item_stock) 中处理的:

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;
}
Run Code Online (Sandbox Code Playgroud)

所以我要过滤掉的是“ (%2$s in stock)”部分。但我找不到任何过滤器。

axe*_*a82 1

谢谢@LoicTheAztec 的回复,但我实际上找到了一个过滤器,woocommerce_add_error

所以我的最终过滤器(在functions.php中)是这样的:

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' );
Run Code Online (Sandbox Code Playgroud)

这应该可以全面解决这个问题。

笔记!我还发现输入框有一个最大属性,这反过来意味着任何人仍然可以看到实际的总可用量(通过简单地使用内置增量(达到最大值时将停止)或只是输入到最高值)一个值,单击更新购物车,您将收到一条通知,金额必须等于或小于 X(最大值))。

为了解决这个问题,我在已有的“woo-xtra.js”中添加了一个简单的 JS:

var qty             = $('form.woocommerce-cart-form').find('input.qty');
// Reset max value for quantity input box to hide real stock
qty.attr('max', '');
Run Code Online (Sandbox Code Playgroud)

这样就没有最大值,但用户仍然会从上面得到错误(如果超过限制):)

即问题解决了