【问题标题】:Get the billing state code to display the name in WooCommerce获取帐单状态代码以在 WooCommerce 中显示名称
【发布时间】:2017-08-13 23:39:56
【问题描述】:

我拼命想了解,WooCommerce 如何将某些国家/地区的县/州名称转换为数据库字段常量并返回。

即。

我有一位来自希腊的客户,恰好来自某个县/州,我在这个键盘上没有找到要命名的字母。

显然,即使 WooCommerce 也没有字母,因为在数据库中,县/州也仅保存为“D”。

我可以使用什么函数将其还原为它的前端形式


编辑 1。

我找到了类似的东西,但我不确定如何使用它。

【问题讨论】:

    标签: php wordpress woocommerce states countries


    【解决方案1】:

    有两种方法可以做到这一点。

    1) 直接使用WC()->countries 对象(如果可用):

    $countries = WC()->countries;
    

    2) 使用WC_Countries 对象的实例:

    $countries_obj = new WC_Countries();
    

    然后你可以使用任何WC_Countries 方法。

    在一个数组中获取所有发货国家代码/名称,并在一个多级数组中获取所有国家代码/名称:

    // Get all countries key/names in an array:
    $countries_array = WC()->countries->get_countries();
    
    // Get all country states key/names in a multilevel array:
    $country_states_array = WC()->countries->get_states();
    

    $countries_obj = new WC_Countries();
    
    // Get all countries key/names in an array:
    $countries_array = $countries_obj->get_countries();
    
    // Get all country states key/names in a multilevel array:
    $country_states_array = $countries_obj->get_states();
    

    例如,如果国家代码是 GR 而州代码是 D

    $countries_obj = new WC_Countries();
    $countries_array = $countries_obj->get_countries();
    $country_states_array = $countries_obj->get_states();
    
    // Get the country name:
    $country_name = $countries_array['GR'];
    
    // Get the state name:
    $state_name = $country_states_array['GR']['D'];
    
    // Display names:
    echo 'Country name: ' . $country_name . ' <br>State name: ' . $state_name;
    

    这将显示:

    国名:希腊
    州名:Ήπειρος

    【讨论】:

    • 感谢上面的代码帮助了我。我有个问题。如果我有一个手动添加的 40 个州名称,要让每个州都回显,我必须为每个州添加 ($state_name = $country_states_array['GR']['D'];)钥匙”?
    【解决方案2】:

    嗯……

    看来你要加载国家和县

    $wcCountries = new WC_Countries();
    $wcCountries->load_country_states();
    

    因此 states 全局变量变得可用

    global $states
    

    因此您可以通过

    检索您的完整州名
    $countyCode = isset($states[$countryCode][$countyCode]) ? $states[$countryCode][$countyCode] : $countyCode;
    

    因为一些县代码无论如何都使用他们的全名。

    编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      相关资源
      最近更新 更多