【问题标题】:How to add new state(s) for a country to WooCommerce?如何将一个国家的新州添加到 WooCommerce?
【发布时间】:2020-08-07 09:02:59
【问题描述】:

我想在 WooCommerce 中添加新状态。我使用了 woo-state 和 sn-p 插件并添加了以下代码:

function woo_add_my_country( $country ) {
   $country["AE-DU"] = 'Dubai';
   return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 ); 

但我还是看不到它。

如何向 WooCommerce 添加国家/地区的新州?

【问题讨论】:

    标签: php wordpress woocommerce state country-codes


    【解决方案1】:

    你没有使用正确的钩子和方法来做到这一点。请改用以下内容:

    add_filter('woocommerce_states', 'add_country_states');
    function add_country_states( $states ) {
        // If states already exist for "AE" country (add it to existing ones)
        if( isset($states['AE'] ) ) {
            $states['AE']['DU'] = __('Dubai', 'woocommerce');
        }
        // IF states  doesn't exist for "AE" country add the new states
        else {
            // One state by line in the array with its code as key
            $states['AE'] = array(
                'DU' => __('Dubai', 'woocommerce'),
            );
        }
        return $states;
    }
    

    您可以在“AE”国家代码的选择字段中添加一个占位符,例如(可选):

    add_filter('woocommerce_get_country_locale', 'filter_get_country_locale');
    function filter_get_country_locale( $country_locale ) {
        $country_locale['AE']['state']['placeholder'] = __('Select a state', 'woocommerce');
    
        return $country_locale;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    所有相关questions and answers using woocommerce_states hook

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多