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

标签 php wordpress woocommerce cart stock

在 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/

相关文章:

php - 如何在sql中获取多个重复值计数

html - 如何在 wordpress 中自定义侧边栏并在其中添加可折叠菜单?

php - 投资组合类别 - 如何更改它们在投资组合页面(WordPress 网站)上的显示顺序

php - Woocommerce - 是否可以将可变产品添加到分组产品中?

php - 使用附加列将 PHP 数组放入 MySQL

php - 通过 id 从 mysql 选择所有行并进行两列比较

php - 计算数组列中的唯一值

html - 文本对齐图像不工作的CSS

php - 在 WooCommerce 中按产品 ID 显示带有短代码的产品价格

php - 在 WooCommerce 帐户激活邮件中访问用户的名字(DOI 通过 WooCommerce Germanized)