Iin WooCommerce,我已将woocommerce-> settings-> products-> inventory->库存显示格式设置为“从不显示剩余库存量”.
但是,如果客户将产品广告到购物车,继续购物或结帐页面并输入高于我们库存的值,则会收到以下错误消息:
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.
我可以使用什么过滤器来编辑此输出?我不希望它显示(绝对在前端商店的任何地方)实际可用的库存量.
我发现这是在第491行的函数(check_cart_item_stock),[root] – > wp-content-> plugins-> woocommerce-> includes-> class-wc-cart.php中处理的:
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)”部分.但我找不到任何过滤器.
解决方法:
感谢@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' );
这应该全面解决.
注意!我还发现输入框有一个最大属性,这反过来意味着任何人仍然可以看到实际的总可用量(通过简单地使用内置增量(当达到最大值时将停止)或只是输入到一个值,单击更新购物车,您将收到一个通知,该金额必须等于或小于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', '');
这种方式没有最大值,但用户仍然会从上面得到错误(如果超过限制):)
即问题解决了