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:
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?
You can access the
WC_Countries
class withWC()->countries
. So to get the country name from an order, you must use:On WooCommerce 3.0+ you should use:
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:
To get the state name from state code you can use.
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
First you have set product tag “VIP or what ever you like and then you will add it to code