在 Woocommerce 结帐页面中添加城市下拉列表

2021-12-22 00:00:00 dropdown php wordpress woocommerce checkout

我想在 Woocommerce 结帐页面的下拉列表中添加特定城市列表.

I want to add specific cities list in dropdown in checkout page of Woocommerce.

添加特定城市下拉菜单的最佳解决方案.

Any best solution to add a dropdown of specific cities.

我想在这个网站上添加下拉菜单http://www.pkbrand.com

I want to add dropdown on this website http://www.pkbrand.com

推荐答案

可以使用钩在 woocommerce_checkout_fields 动作钩子中的自定义函数来完成,您将在其中定义一个你想要的城市的数组:

It can be done using a custom function hooked in woocommerce_checkout_fields action hook, where you will define in an array your desired cities:

// 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' ),
        'Karachi' => 'Karachi',
        'Lahore' => 'Lahore',
        'Faisalabad' => 'Faisalabad',
        'Rawalpindi' => 'Rawalpindi',
        'Gujranwala' => 'Gujranwala',
        'Peshawar' => 'Peshawar',
        'Multan' => 'Multan',
        'Hyderabad' => 'Hyderabad',
        'Islamabad' => 'Islamabad'
    );

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

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

经过测试并有效.

相关文章