【问题标题】:How to change "Have a coupon?" text in Woocommerce checkout page如何更改“有优惠券?” Woocommerce 结帐页面中的文本
【发布时间】:2019-03-01 16:27:21
【问题描述】:

我正在尝试翻译“有优惠券吗?”使用此代码在 Woocommerce 结帐页面中输入文本:

function custom_strings_translation( $translated_text, $text, $domain ) {
  switch ( $translated_text ) {
    case 'HAVE A COUPON?' : 
        $translated_text =  __( 'TIENES UN CUPÓN?', '__x__' );
        break;
}

  return $translated_text;
}
add_filter('gettext', 'custom_strings_translation', 20, 3);

但它不起作用。

谁能告诉我为什么?

【问题讨论】:

    标签: php wordpress woocommerce checkout gettext


    【解决方案1】:

    它不起作用,因为文本不是大写的,而且您没有使用正确的变量。

    有两种方法可以更改此文本(第二种方法似乎最好)

    1) 以这种方式使用gettext 过滤钩子:

    add_filter('gettext', 'custom_strings_translation', 20, 3);
    function custom_strings_translation( $translated_text, $text, $domain ) {
        if( $text == 'Have a coupon?' ){
            $translated_text =  __( 'Tienes un cupón?', $domain );
        }
        return $translated_text;
    }
    

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


    2) 使用woocommerce_checkout_coupon_message 过滤钩子可以让您进行更多更改:

    add_filter( 'woocommerce_checkout_coupon_message', 'custom_checkout_coupon_message', 20, 1 );
    function custom_checkout_coupon_message( $notice ) {
        return __( 'Tienes un cupón?', 'woocommerce' ) . ' <a href="#" class="showcoupon">' . __( 'Haga clic aquí para ingresar su código', 'woocommerce' ) . '</a>'
    }
    

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


    相关:

    【讨论】:

    • 您好,感谢您的回答。我不知道为什么,但我的问题在这里改变了。我的问题是除了“有优惠券吗?”之外,我还有其他字符串。像“购物车”(这是我的网站:brisaperu.com),我一个都翻译不了。我尝试将您给我的第一个代码用于“购物车”,但仍然无法翻译。
    猜你喜欢
    • 2014-09-08
    • 2021-04-03
    • 1970-01-01
    • 2018-02-22
    • 2020-03-19
    • 2016-02-25
    • 2021-05-21
    • 2017-07-13
    • 2021-02-17
    相关资源
    最近更新 更多