【问题标题】:Getting country name from country code in WooCommerce从 WooCommerce 中的国家代码获取国家名称
【发布时间】:2014-08-27 13:27:03
【问题描述】:

WooCommerce 将国家/地区定义如下(为简洁而编辑):

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' ),
            // […]
        ));
    }
}

下订单时,国家代码会写入 WordPress wp_postmeta 表,并且可以使用get_post_meta() 函数在任何可以访问订单 ID 的地方提取:

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

由于我们只是从数据库中检索两个字符,因此问题是如何将发货国家/地区代码(例如 AF)转换为 WC_Countries 类中指定的国家/地区名称?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以使用WC()->countries 访问WC_Countries 类。因此,要从订单中获取国家/地区名称,您必须使用:

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

    在 WooCommerce 3.0+ 上,您应该使用:

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

    如果你想获得状态,你需要在检查是否存在之前,因为 WooCommerce 不包括所有状态,所以这里你需要什么:

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

    【讨论】:

    • 以及如何获取特定语言的国家名称?例如 sk_SK?
    • 简洁的解决方案,只想提一下它的推荐使用WC()->countries->get_countries()获取国家数组
    【解决方案2】:

    要从州代码中获取州名,您可以使用。

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

    【讨论】:

    • 伟大的补充。我会将其添加到已接受的答案中。
    【解决方案3】:

    我正在寻找如何获取 Woocommerce 商店的基本地址,而不是购物车或订单地址,并且该线程包含与该主题相关的许多 Google 关键字。 我遇到了以下我更新了一点的答案: 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;
    }
    

    【讨论】:

      【解决方案4】:
      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' );
      

      首先您设置产品标签“VIP 或您喜欢的任何内容,然后您将其添加到代码中

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

      在此功能中查找是否有任何带有“VIP”产品标签的产品。 和$cat_check = true;

      然后在这个函数里面我们添加函数 woo_override_checkout_fields( $fields ) 功能设置受限 country 作为发货国家字段。

      内部woo_override_checkout_fields( $fields ) { }

      设置您要显示的国家/地区

      $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)')
      
      );
      

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 2016-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-13
        相关资源
        最近更新 更多