php - 如何更改 Woocommerce 结帐页面中的 "Have a coupon?"文本

标签 php wordpress woocommerce checkout gettext

我正在尝试翻译“Have a coupon?”使用此代码在 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);

但它不起作用。

谁能告诉我为什么?

最佳答案

It doesn't work because the text is not in capitals and you are not using the right variable.

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

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

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 过滤器钩子(Hook)可以让您进行更多更改:

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 文件。经过测试并有效。


相关:

关于php - 如何更改 Woocommerce 结帐页面中的 "Have a coupon?"文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508477/

相关文章:

php - 执行 ping 命令在 Linux Centos 上不起作用

PHP mysqli 插入不工作,但没有给出任何错误

wordpress - Woocommerce 分页 - 防止第 1 页上的 301 重定向

php - oci_bind_by_name RETURNING INTO 截断值

php - 如何用php和mysql制作全文搜索引擎?

linux - Wordpress 在更新中使用 'www-data' 作为用户 - 如何将其更改为另一个用户?

javascript - WordPress自定义插件按钮onclick调用php函数

php - 如何在 PHPUnit 中的测试之间共享夹具

php - 在 WooCommerce 结帐表单中显示自定义表格值

php - 在 WooCommerce 上将 'Buy Now' 按钮定位在我的 'Featured Products' 小部件下方?