Home » Php » php – I want a Drop down list depends on country selection in woo commerce

php – I want a Drop down list depends on country selection in woo commerce

Posted by: admin February 25, 2020 Leave a comment

Questions:

Here I have used this code to show Dropdown list. But I want to hide this Dropdown list and show default field except “Sri Lanka”.

If they choose except Sri lanka. It should be default. no need any dropdown.
Because i set shipping country must be Sri lanka only.

// 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' ),
    'Batticaloa' => 'Batticaloa',
    'Kattankudy' => 'Kattankudy',
    'Araipattai' => 'Araipattai',
);

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

add_filter( 'woocommerce_shipping_fields' , 'customize_shipping_postcode_field' );
function customize_shipping_postcode_field( $shipping_fields ) {

$shipping_fields['shipping_postcode']['type'] = 'select';
$shipping_fields['shipping_postcode']['options'] = array(
    ''         => __('Select your postcode', 'woocommerce'),
    '30000' => 'Batticaloa-30000',
    '30100' => 'Kattankudy-30100',
    '30150' => 'Araipattai-30150',
);

return $shipping_fields;
}

add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {

$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['options'] = array(
    ''         => __('Select your postcode', 'woocommerce'),
    '30000' => 'Batticaloa-30000',
    '30100' => 'Kattankudy-30100',
    '30150' => 'Araipattai-30150',
);

return $adresses_fields;
}
How to&Answers: