WooCommerce: Pre-select and restrict to one state and city on checkout WooCommerce: Pre-select and restrict to one state and city on checkout wordpress wordpress

WooCommerce: Pre-select and restrict to one state and city on checkout


Since woocommerce 3, default_checkout_country and default_checkout_state filter hooks are now deprecated and replaced.

Also in your last function, if you want to restrict just to one city, you should not include your city in the array of states. Instead you should return only one possible value.

So this will be your code:

// default checkout countryadd_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );function change_default_checkout_country() {    return 'PK'; // country code}// default checkout stateadd_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );function change_default_checkout_state() {    return 'SD'; // state code}// Setting one state onlyadd_filter( 'woocommerce_states', 'custom_woocommerce_state', 10, 1 );function custom_woocommerce_state( $states ) {    // Returning a unique state    return array('PK' => array('SD' => 'Sindh'));}// Only one city at checkoutadd_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields', 10, 1 );function custom_checkout_fields( $fields ) {    $fields['billing']['billing_city']['type'] = 'select';    $fields['billing']['billing_city']['options'] = array('Karachi' => 'Karachi');    $fields['shipping']['shipping_city']['type'] = 'select';    $fields['shipping']['shipping_city']['options'] = array('Karachi' => 'Karachi');    return $fields;}

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

This code is tested and works for wooCommerce versions 3+

Once added the code above and saved, you will need in WooCommerce > Settings > General to set locations this way:

enter image description here

Then you will get something like this on checkout:

enter image description here

Both dropdown have only one value… So you get what you are expecting.