Getting country name from country code in WooCommerce

WooCommerce defines countries as follows (edited for brevity):

class WC_Countries {
    public $countries;

    public function __construct() {
        global $woocommerce, $states;

        $this->countries = apply_filters( 'woocommerce_countries', array(
            'AF' => __( 'Afghanistan', 'woocommerce' ),
            'AX' => __( 'Åland Islands', 'woocommerce' ),
            'AL' => __( 'Albania', 'woocommerce' ),
            'DZ' => __( 'Algeria', 'woocommerce' ),
            // […]
        ));
    }
}

When an order is placed, the country code is written to the WordPress wp_postmeta table and can be extracted anywhere an order id can be accessed using the get_post_meta() function:

Read More
get_post_meta( $order->id, '_shipping_country', true ),

Since we are simply retrieving two characters from the database, the question is how to translate the shipping country code (e.g. AF) to the country name specified in the WC_Countries class?

Related posts

Leave a Reply

5 comments

  1. You can access the WC_Countries class with WC()->countries. So to get the country name from an order, you must use:

    WC()->countries->countries[ $order->shipping_country ];
    

    On WooCommerce 3.0+ you should use:

    WC()->countries->countries[ $order->get_shipping_country() ];
    

    If you like to get the state, you need before check if exist, since WooCommerce doesn’t include all states, so here what do you need:

    $states = WC()->countries->get_states( $order->get_shipping_country() );
    $state  = ! empty( $states[ $order->get_shipping_state() ] ) ? $states[ $order->get_shipping_state() ] : '';
    
  2. I was looking how to get the Woocommerce shop base address, not the cart or an order address, and this thread comes in many Google keywords related to the topic.
    I came accross the following answer which I updated a little :
    https://wordpress.stackexchange.com/a/334608/177690

    private static function afg_getBaseAddressData() {
        return array(
            'address' => WC()->countries->get_base_address(),
            'address-2' => WC()->countries->get_base_address_2(),
            'city' => WC()->countries->get_base_city(),
            'zip' => WC()->countries->get_base_postcode(),
            'state' => WC()->countries->get_base_state(),
            'country' => WC()->countries->countries[WC()->countries->get_base_country()],
            'mail' => self::afg_getPublicMail()
        );
    }
    
    private static function afg_getPublicMail() {
        //replace by the way you get an email address
        return filter_var(get_option('address-public-mail'), FILTER_VALIDATE_EMAIL);
    }
    
    public static function afg_getAddressTemplate() {
        $datas = apply_filters( 'afg_shop_base_address', self::afg_getBaseAddressData() );
        $html = '';
    
        $html .= '<address>'; 
        foreach ($datas as $key => $data) {
            if($data) {
                $html .= '<p class="address-line '.$key.'">'.$data.'</p>';
            }
        }
        $html .= '</address>';
    
        return $html;
    }
    
  3. function a000_remove_bundles_counting(){
    //////////////////////////////
    global $woocommerce_bundles;
    remove_filter( 'woocommerce_cart_contents_count',
    array( $woocommerce_bundles->display, 'woo_bundles_cart_contents_count' ) );
    }
    add_action( 'init', 'a000_remove_bundles_counting' );
    ///////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////
    
    function d000_cart_contents_count( $count ) {
        global $woocommerce;
     $cat_check = false;
      foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) { //foreach
    
     $product = $cart_item['data'];
    
     if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
    $cat_check = true;
    // break because we only need one "true" to matter here
    if (!function_exists('woo_override_checkout_fields')) {
    function woo_override_checkout_fields( $fields ) { // woo_override_checkout_fields Function
    
    
    $fields['billing']['billing_country'] = array(
    'type' => 'select',
    'label' => __('Country', 'woocommerce'),
    'options' => array('US' => 'United States(US)')
    ); 
    
    $fields['billing']['billing_state'] = array(
    'type' => 'select',
    'label' => __('State', 'woocommerce'),
    'options' => array('CA' => 'California(CA)')
    
    );
    
    return $fields; 
    } //end woo_override_checkout_fields Function
    }
    add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );
    
    } // end search product_cat 
      }// end foreach
    
    
    
    return $count;
    }
    add_filter( 'woocommerce_cart_contents_count',
    'd000_cart_contents_count' );
    

    First you have set product tag “VIP or what ever you like and then you will add it to code

    if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat 
    
        $cat_check = true;
    }
    

    in this function looking is there any product with “VIP” Product tag.
    and $cat_check = true;

    then inside this in function we adding function
    woo_override_checkout_fields( $fields ) function to set limited
    country as shipping country field.

    Inside woo_override_checkout_fields( $fields ) { }

    Set which country you want to show

    $fields['billing']['billing_country'] = array(
    'type' => 'select',
    'label' => __('Country', 'woocommerce'),
    'options' => array('US' => 'United States(US)')
    );
    
    
    
    $fields['billing']['billing_state'] = array(
    'type' => 'select',
    'label' => __('State', 'woocommerce'),
    'options' => array('CA' => 'California(CA)')
    
    );