Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the full country name rather than country code in WooCommerce?

Trying to use <?php echo $current_user->billing_country; ?> to display the WooCommerce user billing country. This for example echo’s as AU if the customer has an Australian billing address. However, I would like to pull the full name in place of the country code.

The same applies for the billing_state. Instead of getting South Australia I get SA. Have made work through functions.php, however only if I add each state and country. This is obviously a great undertaking to map the worlds states to the state codes.

I believe I have done this before in a very simple way, but for whatever reason cannot figure it out at this moment.

The country and state are being used within a disable input field as the value

<input type="text" class="form-control" value="<?php echo $current_user->billing_country; ?>" disabled="">

Image of what we currently get

Image of what we currently get

Image of what we want to get Image of what we want to get

like image 618
Darren Avatar asked Aug 11 '17 11:08

Darren


1 Answers

WooCommerce has WC_Countries class for this you can use it like this:

echo WC()->countries->countries['AU']; //To get country name by code
echo WC()->countries->states['AU']['SA']; //to get State name by state code

OR (in your case)

echo WC()->countries->countries[$current_user->billing_country]; //To get country name by code
echo WC()->countries->states[$current_user->billing_country][$current_user->billing_state]; //to get State name by state code

Hope this helps!

like image 150
Raunak Gupta Avatar answered Oct 31 '22 20:10

Raunak Gupta