下拉自动值 country/state/city WooCommerce 店面

Drop down of auto values country/state/city WooCommerce storefront

我正在寻找 country/state/city 的下拉菜单。我正在让 country state 自动填充,但城市运行不佳。默认情况下,我可以获取国家/地区。

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

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

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

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 文件中或任何插件文件中。

已测试并有效。这次你会得到它:

现在要让城市自动填充你应该使用这个:

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 文件中或任何插件文件中。

经过测试广告有效……

但是您不会按州获得城市,因为这是一个真正的发展,对于 Stack Overflow 来说太宽泛了