Change WooCommerce state and city checkout fields to dropdowns related to the chosen country

admin2025-01-07  3

When I select a country WooCommerce checkout, how can I have state and city fields as dropdowns with the related data, like "when user select india then in state field shows only indian states". Please suggest what to do.

When I select a country WooCommerce checkout, how can I have state and city fields as dropdowns with the related data, like "when user select india then in state field shows only indian states". Please suggest what to do.

Share Improve this question edited Apr 27, 2019 at 23:46 LoicTheAztec 3,38117 silver badges24 bronze badges asked Apr 27, 2019 at 13:56 Hakimuddin SaifeeHakimuddin Saifee 392 silver badges10 bronze badges 1
  • 1 Kindly, but you could search a bit before asking: Google search … so there is also the following plugins: States, Cities, and Places for Woocommerce and WC City Select … There are also a lot of threads related with code snippets… – LoicTheAztec Commented Apr 27, 2019 at 22:09
Add a comment  | 

1 Answer 1

Reset to default 0

although it's true that you can search on google, that's probably why you're here in the first place. The plugins mentioned are alternatives, but if you still wanna do it, here's how to turn the city field into a dropdown:

/**
 * Change the checkout city field to a dropdown field.
 */
function jeroen_sormani_change_city_to_dropdown( $fields ) {

    $city_args = wp_parse_args( array(
        'type' => 'select',
        'options' => array(
            'city1' => 'name city 1',
            'city2' => 'name city 2',
            'city3' => 'name city 3',
        ),
    ), $fields['shipping']['shipping_city'] );

    $fields['shipping']['shipping_city'] = $city_args;
    $fields['billing']['billing_city'] = $city_args; // Also change for billing field

    return $fields;

}
add_filter( 'woocommerce_checkout_fields', 'jeroen_sormani_change_city_to_dropdown' );

ps: I use this code myself, but i claim no ownership over it as i did not write it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256112a335.html

最新回复(0)