Getting country name from country code in WooCommerce

WordPress WooCommerce defines a country class 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 placing an order,

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

The question is how to simply pull out two characters from the database, how can I translate the code of the country of delivery (for example, AF) into the name of the country indicated in the "Countries" class?

+5
source share
4 answers

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

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

In WooCommerce 3.0+, you should use:

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

If you like to receive state, you need to check whether it exists before WooCommerce does not include all states, so here is what you need:

 $states = WC()->countries->get_states( $order->get_shipping_country() ); $state = ! empty( $states[ $order->get_shipping_state() ] ) ? $states[ $order->get_shipping_state() ] : ''; 
+15
source

To get the status name from the status code, you can use.

 WC()->countries->states[$order->billing_country][$order->billing_state]; 
+3
source

The main class is the Woocommerce class, accessible globally through the $ woocommerce variable. It handles the main functions of WooCommerce, in other classes, stores site variables and processes error / success messages.

Follow this http://www.phpwala.in/woocommerce/get-full-country-name-from-country-code-in-woocommerce/2015/11 for more details.

0
source
 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 set the product tag to “VIP” or whatever you like, and then you add it to the code

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

in this function is there any product with the product tag "VIP". and $cat_check = true;

then inside this, the function woo_override_checkout_fields( $fields ) is added to the function woo_override_checkout_fields( $fields ) function is limited to the country as the country of delivery.

Inside woo_override_checkout_fields( $fields ) { }

Indicate in 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)') ); 
-1
source

Source: https://habr.com/ru/post/1201231/


All Articles