【问题标题】:How to conditionally change WooCommerce billing address title if no separate shipping address is entered如果没有输入单独的送货地址,如何有条件地更改 WooCommerce 帐单地址标题
【发布时间】:2020-08-15 00:10:21
【问题描述】:

我的网站有两个选项

  • 您可以运送到帐单邮寄地址
  • 您可以输入帐单地址和单独的送货地址,非常标准。

如果您在结帐时同时输入帐单地址和送货地址,则在“我的帐户”的订单详细信息页面中,您可以在相关地址上方看到帐单地址和送货地址标题。

如果您在结帐时只输入帐单地址,则在我的帐户的订单详情页面中您只能看到“帐单”地址,该地址不会与送货地址重复。没有显示送货地址,所以看起来很奇怪。


如果订单没有单独的送货地址,我想有条件地将“账单地址”重命名为“账单和送货地址”。

在电子邮件中更改它也是有益的,因为它会更有意义。

我找到了这个用于重命名标题(我还没有测试过),但是我怎样才能有条件地做呢?

function wc_billing_field_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Billing address' :
            $translated_text = __( 'Billing & Shipping address', 'woocommerce' );
            break;
    }

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

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    我的帐户

    https://github.com/woocommerce/woocommerce/blob/master/templates/order/order-details-customer.php

    • 可以通过将其复制到 yourtheme/woocommerce/order/order-details-customer.php 来覆盖此模板。

    替换(行:31

    <h2 class="woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
    

    <h2 class="woocommerce-column__title">
    <?php
    // Show shipping = false    
    if ( !$show_shipping ) {
        $title = 'Billing & Shipping address';      
    } else {
        $title = 'Billing address';
    }
    
    esc_html_e( $title, 'woocommerce' );
    ?>
    </h2>
    

    电子邮件

    https://github.com/woocommerce/woocommerce/blob/master/templates/emails/email-addresses.php

    • 可以通过将其复制到 yourtheme/woocommerce/emails/email-addresses.php 来覆盖此模板。

    替换(行:29

    <h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
    

    <h2>
    <?php 
    // Billing_address
    if ( !$order->needs_shipping_address() ) {
        $title = 'Billing & Shipping address';     
    } else {
        $title = 'Billing address';
    }
    
    esc_html_e( $title, 'woocommerce' );
    ?>
    </h2>
    

    【讨论】:

    • 谢谢 - 不知道为什么我要尝试使用函数而不是子主题模板来执行此操作...我认为电子邮件部分需要使用函数来完成?
    • 再次感谢,我查看的是 WooCommerce 中的电子邮件模板,而不是插件中的模板文件。
    • @Lyall,已针对电子邮件进行了更新。请注意,您可以根据 if 条件中使用的代码添加多个检查(电子邮件模板),例如 wc_ship_to_billing_address_only()$order-&gt;needs_shipping_address()$shipping
    • 我似乎无法让电子邮件工作 - 您发布的第一个版本正确更改了它,但它改变了所有内容,更新版本似乎没有改变它?我的帐户部分完美运行:)
    • 完美 - 谢谢!感谢您提供有关其他检查的信息,我会记住这些以备将来使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2013-01-20
    相关资源
    最近更新 更多