Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

php - Add dropdown list of cities in Woocommerce checkout page

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

Any best solution to add a dropdown of specific cities.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...