1

I have added a custom list of states to my woocommerce using the code here: https://docs.woocommerce.com/document/addmodify-states/

The newly added states load fine on frontend and some backend screens, however in e-mails and user account screen, woocommerce only loads the the code / value instead of the actual name. (XX1, XX2 etc.)

I believe I can fix it using this logic:

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

So I was wondering if it's possible to use this logic to create a function that would print the state name whenever the template calls the code? Any help would be very much appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
eylul
  • 91
  • 1
  • 4
  • 13

1 Answers1

6

You can use the following from the current user (WP_User object) to get the state label name:

$user = wp_get_current_user(); // The current user

$country = $user->billing_country;
$state = $user->billing_state;
echo WC()->countries->get_states( $country )[$state];

Tested and works


And for an order from the Order ID:

$order = wc_get_order($order_id); // The WC_Order object

$country = $order->get_billing_country();
$state = $order->get_billing_state();
echo WC()->countries->get_states( $country )[$state];
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you very much, but I think for this to work I need to find every piece of line that calls the state in every plugin and template and replace it with the code above. Do you think it would be possible to turn this into a function that changes the state codes to names sitewide? – eylul Jun 12 '18 at 07:40
  • @eyful You can only replace the displayed state code into that label name in the necessary locations, as they should not be a lot. I don't think that you can make a function that will changes the state codes to names sitewide. It need to be done case by case and carefully. – LoicTheAztec Jun 13 '18 at 01:28
  • 1
    I understand. Thank you very much for your help! – eylul Jun 14 '18 at 10:22