从WooCommerce购物车错误消息中删除库存数量 [英] Remove the stock quantity from WooCommerce cart error messages

查看:82
本文介绍了从WooCommerce购物车错误消息中删除库存数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我已经将woocommerce->设置->产品->库存->库存显示格式设置为从不显示剩余库存量

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:


抱歉,我们没有足够的 {product_name} 库存来满足您的订单( {available_stock_amount} 库存)。请编辑您的购物车,然后重试。对于给您带来的任何不便,我们深表歉意。

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.

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

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

所以我要过滤的是 (%2 $ s stock)部分。

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

推荐答案

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

Thank you @LoicTheAztec for your reply, but I actually found a filter for this after all, woocommerce_add_error

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

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' );

这应该全面解决它。

注意!,我还发现输入框具有 max 属性,这又意味着任何人仍然可以看到实际的总可用金额(只需使用内置增量(当达到最大值时将停止),或者只是输入一个高值,请单击更新购物车,您会注意到该数量必须等于或小于X(最大值)。

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)).

为了解决这个问题,我在先前存在的 woo-xtra.js中添加了一个简单的JS:

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 :)

即问题已解决

这篇关于从WooCommerce购物车错误消息中删除库存数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆