【问题标题】:How to change "Billing details" title on WooCommerce checkout page with a hook如何使用钩子更改 WooCommerce 结帐页面上的“帐单详细信息”标题
【发布时间】:2026-01-16 10:25:01
【问题描述】:

我正在尝试更改 WooCommerce 结帐页面上的一些标题。

其中一个标题是“帐单明细”

我试过了:

function wc_billing_field_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Billing details' :
            $translated_text = __( 'Billing Info', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );

我无法更改这些文本,无论我添加到我的 functions.php 或 WooCommerce 更改文件中。

能否请您告诉我,我该如何更改这些标题?

注意:我想使用动作挂钩。我不会像其他选项建议的那样复制 WooCommerce 模板文件。

【问题讨论】:

  • 您是否检查过您的主题是否超过了默认模板? /wp-content/themes/yourtheme/woocommerce/checkout/form-billing.php 您提到的方法在使用默认 Woocommerce 模板时应该有效,因为该文件的第 23 行如下<h3><?php esc_html_e( 'Billing details', 'woocommerce' ); ?></h3> 函数 esc_html_e 使用 gettext
  • 嗨!谢谢您的回答。但我没有使用我自己的模板文件来覆盖 Woocommerce 的模板文件。我所做的一切都是为了自定义我的主题,就是使用动作钩子。
  • 在这种情况下,您使用 gettext 的方法应该可以正常工作,您可以尝试改用$text 吗?并尝试使用更高的优先级 99 而不是 20 例如
  • @ThomasTromp 你确定你在你的活动主题functions.php文件中添加了吗?因为我刚刚测试过并且工作正常。

标签: php wordpress woocommerce hook-woocommerce checkout


【解决方案1】:

在您的代码中将switch ( $translated_text ) { 更改为switch ( $text ) {。 这是因为$text 包含原始(翻译不足)文本,而$translated_text 包含...变量的名称已经表明它。


或者使用

function filter_gettext( $translated, $original_text, $domain ) {   
    // Is admin
    if ( is_admin() ) return $translated;
    
    // No match
    if ( $original_text != 'Billing details' ) return $translated;
    
    // Match
    $translated = __( 'Billing Info', $domain );
    
    return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );

【讨论】:

    最近更新 更多