下载自动值国家/州/城市woocommerce店面

时间:2017-11-17 11:50:46

标签: php wordpress woocommerce checkout states

寻找国家/州/市的下拉列表。我正在让国家自动填充,但城市不能很好地通过。默认能够获得国家。

          // State-Country additions

      /**
       * Code goes in functions.php or a custom plugin.
       */

      add_filter('woocommerce_states', 'SA_woocommerce_states');
      function SA_woocommerce_states( $states ) {
          $states['ZA'] = array(
              'EC' => __('Eastern Cape', 'woocommerce'),

          );
          return $states;
      }

      // Change "city" checkout billing and shipping fields to a dropdown

      add_filter('woocommerce_checkout_fields', 'override_checkout_city_fields');
      function override_checkout_city_fields($fields) {

          // Define here in the array your desired cities (Here an example of cities)
          $option_cities = array(
              '' => __( 'Select your city' ),
              'a' => 'a',

          );

          $fields['billing']['billing_city']['type'] = 'select';
          $fields['billing']['billing_city']['options'] = $option_cities;
          $fields['shipping']['shipping_city']['type'] = 'select';
          $fields['shipping']['shipping_city']['options'] = $option_cities;

          return $fields;
      }

1 个答案:

答案 0 :(得分:0)

使用您的实际代码,您将一个州取代所有现有的“南非”(ZA)州。所以你得到的东西是:

enter image description here

要添加此状态,您需要以这种方式更改代码:

add_filter('woocommerce_states', 'sa_woocommerce_states');
add_filter('woocommerce_countries_allowed_country_states', 'sa_woocommerce_states');
function SA_woocommerce_states( $states ) {
    $states['ZA']['EC'] = __('Eastern Cape', 'woocommerce');
    return $states;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

经过测试和工作。这次你会得到它:

enter image description here

现在要自动填充城市,你应该使用它:

add_filter( 'woocommerce_default_address_fields', 'override_checkout_city_fields', 10, 1 );
function override_checkout_city_fields($fields) {

    // Define here in the array your desired cities (Here an example of cities)
    $option_cities = array(
        '' => __( 'Select your city' ),
        'a' => 'a',

    );

    $fields['city']['type'] = 'select';
    $fields['city']['options'] = $option_cities;

    return $fields;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

测试广告有效......

  

但你不会得到各州的城市,因为这是一个真正的发展,而且对于StackOverFlow来说太宽泛了