Display the full country name rather than country code in WooCommerce?
Asked Answered
S

1

7

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

Saeger answered 11/8, 2017 at 11:33 Comment(0)
O
20

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!

Osculate answered 11/8, 2017 at 14:42 Comment(4)
Thank you very much. Worked a treat!Saeger
@DarrenMathers: don't forget to upvote my answer, as it helps you!Osculate
If the states have not been loaded, try WC()->countries->get_states( $current_user->billing_country )[ $current_user->billing_state ].Singleness
this helped a lot. much obliged sireIndwell

© 2022 - 2024 — McMap. All rights reserved.