How to conditionally change WooCommerce billing address title if no separate shipping address is entered

China☆狼群 提交于 2020-05-16 22:01:12

问题


My site has two options

  • you can ship to the billing address
  • you can enter a billing address and a separate delivery address, pretty standard.

If you enter both a billing and a shipping address during checkout, then in the order details page in My Account you can see both Billing address and Shipping address titles above the relevant addresses.

If you only enter a billing address during checkout, in the order details page in My Account you can only see 'Billing' address, the address isn't duplicated as the shipping address. No Shipping address is shown, so it looks strange.


I would like to conditionally rename 'Billing address' to 'Billing & Shipping address' if the order has no separate shipping address.

It would be beneficial to change it in the emails as well, as it would make more sense.

I have found this for renaming the title (I haven't tested it), but how can I do it conditionally?

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 );

回答1:


My Account

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

  • This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-customer.php.

Replace (line: 31)

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

With

<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>

E-mail

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

  • This template can be overridden by copying it to yourtheme/woocommerce/emails/email-addresses.php.

Replace (line: 29)

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

With

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

esc_html_e( $title, 'woocommerce' );
?>
</h2>


来源:https://stackoverflow.com/questions/61526630/how-to-conditionally-change-woocommerce-billing-address-title-if-no-separate-shi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!