代码之家  ›  专栏  ›  技术社区  ›  Ferna

汽车价值下拉列表国家/州/城市商业店面

  •  0
  • Ferna  · 技术社区  · 7 年前

    // 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 回复  |  直到 3 年前
        1
  •  1
  •   Peter Mortensen icecrime    3 年前

    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;
    }
    

    代码进入 作用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;
    }
    

    代码进入 作用php 活动子主题(或主题)的文件或任何插件文件。

    这是测试和工作

    但你不会按州划分城市,因为这是一个真正的发展,而且对你来说太广泛了