2

inspired by: Get state name instead of the code in Woocommerce Display a preview of the custom field data in the Edit Billing and Shipping Address section on the my account page And in the answers of the friend @LoicTheAztec I intend to show the name of the country in the section (Edit Billing and Shipping Address) of the page my account with the following code, but it only shows me the country code instead of the full country name:`

<?php
$custom_user_meta = get_user_meta(get_current_user_id(), 'billing_country', true);

if( ! empty($custom_user_meta) )
    { ?>
<p> <?php
printf( '<strong>Country / Region:</strong> ' . esc_html( '%s' ), esc_html($custom_user_meta)  );?> 
</p> <?php 
}
?>

` I also require it in the same way to show the name of the shipping country.

I appreciate a lot in what you could guide me please.

Roman
  • 2,530
  • 2
  • 27
  • 50
Williams
  • 421
  • 4
  • 18
  • I believe this answer your question [Getting country name from country code in WooCommerce](https://stackoverflow.com/questions/25528454/getting-country-name-from-country-code-in-woocommerce). So you get `echo WC()->countries->countries[ $custom_user_meta ];` – 7uc1f3r Aug 13 '20 at 16:28
  • Hello @7uc1f3r not because that post refers to obtaining the country of the order. I need to get the name of the billing and shipping country for the current user. – Williams Aug 13 '20 at 16:33
  • well, instead of using `$order->get_shipping_country()`, you could use `WC()->countries->countries[ $custom_user_meta ];`. – 7uc1f3r Aug 13 '20 at 16:34
  • @7uc1f3r that shows nothing. you can add the complete code please. – Williams Aug 13 '20 at 16:36

1 Answers1

3

You could use WC()->countries->countries[ 'COUNTRY CODE' ];

<?php
$custom_user_meta = get_user_meta(get_current_user_id(), 'billing_country', true);

if( ! empty($custom_user_meta) ) {
?>
    <p>
    <?php
    // Get country name
    $country_name = WC()->countries->countries[ $custom_user_meta ];
    
    printf( '<strong>Country / Region:</strong> ' . esc_html( '%s' ), esc_html($country_name) );
    ?> 
    </p>
    <?php 
}
?>
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50