【问题标题】:Change cart totals title text on cart page in Woocommerce在 Woocommerce 的购物车页面上更改购物车总计标题文本
【发布时间】:2019-01-31 11:46:56
【问题描述】:

我希望在 WooCommerce 的购物车总数 div 中更改文本“购物车总数”,或通过操作将其完全删除。

我在使用的方框上方添加了不同的文字

add_action( 'woocommerce_before_cart_totals', 'custom_before_cart_totals' );
function custom_before_cart_totals() {
        echo '<h2>Checkout</h2>';                                                
}

但除了编辑 WooCommerce 模板或目标并使用 css 隐藏之外,我找不到删除默认措辞“购物车总计”的方法,但我希望我可以在函数文件中放置一些东西来更改旧文本或完全删除它。

任何建议将不胜感激。

Default Cart Totals Example

【问题讨论】:

  • 嗨,Donny Dug,WooCommerce 没有提供任何 Hook 来更改购物车总计文本。您可以通过 css 隐藏此文本。或者您需要更改 WooCommerce 购物车页面模板文件中的文本..
  • 是的,这就是问题所在,但是 Loics 的回答效果很好!

标签: php wordpress woocommerce cart hook-woocommerce


【解决方案1】:

可以使用 WordPress 过滤器挂钩 gettext

1) 删除“购物车总数”:

add_filter( 'gettext', 'change_cart_totals_text', 20, 3 );
function change_cart_totals_text( $translated, $text, $domain ) {
    if( is_cart() && $translated == 'Cart totals' ){
        $translated = '';
    }
    return $translated;
}

2) 替换(或更改)“购物车总数”:

add_filter( 'gettext', 'change_cart_totals_text', 20, 3 );
function change_cart_totals_text( $translated, $text, $domain ) {
    if( is_cart() && $translated == 'Cart totals' ){
        $translated = __('Your custom text', 'woocommerce');
    }
    return $translated;
}

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

或者您可以将其从 Woocommerce 模板中删除 cart/cart_totals.php

【讨论】:

  • Legend,这正是我们所寻找的,它完美运行。谢谢!
  • 有什么方法可以同时删除&lt;h2&gt;&lt;/h2&gt;吗?
【解决方案2】:
function change_cart_totals($translated){
  $translated = str_ireplace('Cart Totals', 'Cart Total', $translated);
  return $translated;
}
add_filter('gettext', 'change_cart_totals' );

【讨论】:

  • 本网站上通常不赞成仅使用代码的答案。您能否编辑您的答案以包含一些 cmets 或对您的代码的解释?解释应该回答这样的问题:它有什么作用?它是如何做到的?它去哪儿了?它如何解决OP的问题?请参阅:How to anwser。谢谢!
【解决方案3】:

将 woocommerce 中的 cart-totals.php 主题复制到您自己的主题中,并替换此行:

<h2><?php esc_html_e( 'Cart totals', 'woocommerce' ); ?></h2>

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多