1
votes

I don't understand why get_billing_city() method doesn't work in WC_email customer_details() method, unlike get_billing_country() is working perfectly.

How to make get_billing_city() working in WC_Email customer_details() method to get the correct output in email-customer-details.php WooCommerce template?

1

1 Answers

0
votes

It seem that there is a little bug in woocommerce.

Then as it's an email notification related to Order, you should use get_billing_country() method with WC_Order object instead.

SO for the WC_Order object get_billing_country() method, this can be solved with the turn around below:

add_filter( 'woocommerce_order_get_billing_country','hook_order_get_billing_country', 10, 2);
function hook_order_get_billing_country( $value, $order ){
    // Country codes/names
    $countries_obj = new WC_Countries;
    $country_array = $countries_obj->get_countries();
    // Get the country name from the country code
    $country_name = $country_array[ $value ];
    // If country name is not empty we output it
    if( $country_name )
        return $country_name;
    // If $value argument is empty we get the country code
    if( empty( $value ) ){
        $country_code = get_post_meta( $order->get_id(), '_billing_country', true );
        // We output the country name
        return $country_array[ $country_code ];
    }
    return $value;
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Code is tested and works for wooCommerce versions 3+

So now you should get it working and outputting the correct country name


With WC_Customer object regarding get_billing_country() method, you should use this:

add_filter( 'woocommerce_customer_get_billing_country','wc_customer_get_billing_country', 10, 2);
function wc_customer_get_billing_country( $value, $customer ){
    // Country codes/names
    $countries_obj = new WC_Countries;
    $country_array = $countries_obj->get_countries();
    // Get the country name from the country code
    $country_name = $country_array[ $value ];
    // If country name is not empty we output it
    if( $country_name )
        return $country_name;
    // If $value argument is empty we get the country code
    if( empty( $value ) ){
        $country_code = get_user_meta( $customer->get_id(), 'billing_country', true );
        // We output the country name
        return $country_array[ $country_code ];
    }
    return $value;
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.