在 WooCommerce 中,我将 woocommerce->settings->products->inventory->stock display format 设置为“Never show quantity remaining in stock”。
但是,如果客户将产品广告添加到购物车,继续进入购物车或结帐页面并输入高于我们库存的值(value),他们会收到此错误消息:
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.
我可以使用什么过滤器来编辑此输出?我不希望它显示(绝对是在前端商店的任何地方)实际可用库存量。
我发现这是在函数 (check_cart_item_stock) 中处理的,[root]->wp-content->plugins->woocommerce->includes->class-wc-cart.php 第 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 in stock)
”部分。但我找不到任何过滤器。
最佳答案
显示的消息位于 WC_Cart class source code在第 493 和 522 行……
你可以尝试用你的自定义文本版本替换这个函数中的文本,这个函数卡在 WordPress gettex
过滤器 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;
}
代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。
这应该可以(未经测试)……
关于php - 从 WooCommerce 购物车错误消息中删除库存数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302593/