在WooCommerce中更改添加到购物车库存错误通知

时间:2018-12-15 19:58:20

标签: php wordpress woocommerce product stock

当客户尝试向购物车中添加太多物品时,他们会看到以下错误通知:

  

您不能将金额添加到购物车-我们的库存为X,而您的购物车中已经为Y。

此行为包含在WC_Cart add_to_cart() method源代码at line 1067中。

我想隐藏库存详细信息并替换为:

  

您不能将该金额添加到购物车中-我们的库存不足。

如何在WooCommerce中更改此添加到购物车库存的错误通知,而不会覆盖源代码?

3 个答案:

答案 0 :(得分:2)

可以通过以下方式在自定义的挂钩函数中使用gettext过滤器挂钩:

add_filter( 'gettext', 'custom_add_to_cart_stock_error_notice', 10, 3 );
function custom_add_to_cart_stock_error_notice( $translated, $text, $domain ) {

    if ( $text === 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.' && 'woocommerce' === $domain ) {
        $translated = __("You cannot add that amount to the cart — we don't have enough in stock.", $domain );
    }

    return $translated;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here

答案 1 :(得分:1)

您可以在“说什么” 插件的帮助下更改所说的文本而无需修改插件文件。请按照以下步骤更改文本:

  1. 安装 Here 插件

  2. 访问工具>文本更改

  3. 点击添加新

  4. 根据您的需要填写字段,例如

    • 原始字符串: You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.
    • 文本域: woocommerce
    • 文本上下文:
    • 替换字符串: You cannot add that amount to the cart — we don't have enough in stock.
  5. 点击添加按钮以保存更改。

答案 2 :(得分:0)

请注意@loictheaztec 的代码是有效的,只是如果要使用数量,需要将第四行中的引号更改为单引号

    add_filter( 'gettext', 'custom_add_to_cart_stock_error_notice', 10, 3 );
function custom_add_to_cart_stock_error_notice( $translated, $text, $domain ) {

    if ( $text === 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.' && 'woocommerce' === $domain ) {
        $translated = __('You cannot add that to the cart — we have %1$s in stock and you already have %2$s in your cart.', $domain );
    }