Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Last active April 22, 2024 12:42
Show Gist options
  • Save rynaldos-zz/c95e7f84876fd1496b091c9e3fcfcaa4 to your computer and use it in GitHub Desktop.
Save rynaldos-zz/c95e7f84876fd1496b091c9e3fcfcaa4 to your computer and use it in GitHub Desktop.
Pakistan state/city extension for checkout page and shipping zones. Replaces states for cities
add_filter( 'woocommerce_countries', 'rs_edit_pk_country' );
function rs_edit_pk_country ( $countries ) {
$new_countries = array(
'PK' => __( 'Pakistan', 'woocommerce' ),
);
return array_merge( $countries, $new_countries );
}
add_filter( 'woocommerce_continents', 'rs_add_new_pk_country_to_continents' );
function rs_add_new_pk_country_to_continents ( $continents ) {
$continents['AS']['countries'][] = 'PK';
return $continents;
}
add_filter( 'woocommerce_states', 'custom_wc_states_pk' );
function custom_wc_states_pk ( $states ) {
$states['PK'] = array(
'Azad Kashmir' => 'Azad Kashmir',
'Quetta' => 'Quetta',
'Khuzdar' => 'Khuzdar',
'FATA' => 'FATA',
'Gilgit Baltistan' => 'Gilgit Baltistan',
'Islamabad' => 'Islamabad',
'Peshawar' => 'Peshawar',
'Mardan' => 'Mardan',
'Mingora' => 'Mingora',
'Kohat' => 'Kohat',
'Abbottabad' => 'Abbottabad',
'Dera Ismail Khan' => 'Dera Ismail Khan',
'Nowshera' => 'Nowshera',
'Charsada' => 'Charsada',
'Swabi ' => 'Swabi ',
'Lahore' => 'Lahore',
'Faisalabad' => 'Faisalabad',
'Rawalpindi' => 'Rawalpindi',
'Multan' => 'Multan',
'Gujranwala' => 'Gujranwala',
'Sargodha' => 'Sargodha',
'Sialkot' => 'Sialkot',
'Sheikhupura' => 'Sheikhupura',
'Gujrat' => 'Gujrat',
'Kasur' => 'Kasur',
'Rahim Yar Khan' => 'Rahim Yar Khan',
'Sahiwal' => 'Sahiwal',
'Okara' => 'Okara',
'Wah Cantonment ' => 'Wah Cantonment ',
'Dera Chazi Khan' => 'Dera Chazi Khan',
'Chiniot' => 'Chiniot',
'Kamoke' => 'Kamoke',
'Burewala' => 'Burewala',
'Jhelum' => 'Jhelum',
'Sadiqabad' => 'Sadiqabad',
'Khanewal' => 'Khanewal',
'Hafizabad' => 'Hafizabad',
'Khanpur' => 'Khanpur',
'Gojra' => 'Gojra',
'Bahawalnagar' => 'Bahawalnagar',
'Muridke' => 'Muridke',
'Pakpattan' => 'Pakpattan',
'Jaranwala' => 'Jaranwala',
'Chishtian' => 'Chishtian',
'Daska' => 'Daska',
'Mandi Bahauddin' => 'Mandi Bahauddin',
'Ahmadpur East ' => 'Ahmadpur East ',
'Kamalia' => 'Kamalia',
'Vehari' => 'Vehari',
'Wazirabad' => 'Wazirabad',
'Khushab' => 'Khushab',
'Chakwal' => 'Chakwal',
'Mianwali' => 'Mianwali',
'Kot Adu' => 'Kot Adu',
'Farooka' => 'Farooka',
'Chichawatni' => 'Chichawatni',
'Karachi' => 'Karachi',
'Hyderabad' => 'Hyderabad',
'Sukkur' => 'Sukkur',
'Kandhkot' => 'Kandhkot',
'Larkana' => 'Larkana',
'Mirpur Khas' => 'Mirpur Khas',
'Nawabshah' => 'Nawabshah',
'Jacobabad' => 'Jacobabad',
'Shikapur' => 'Shikapur',
'Tando Adam' => 'Tando Adam',
'Khairpur' => 'Khairpur',
'Dadu' => 'Dadu',
'Tando Allahyar' => 'Tando Allahyar',
);
return $states;
}
@hassan-mujtaba5
Copy link

Bro You are really amazing. You solved my issue. Thanks a lot

@Siln0z
Copy link

Siln0z commented Jul 1, 2021

Thanks a lot ! Worked for me :)
Do you know if it's possible to make the field state "required" directly in this part of code ?
I try many other features, but nothing works.
Thanks again.
Julien

@goldenshawarma
Copy link

Hi, thank you for the code!

I tried to modify the state for my country - PH, and it works like a charm. However, when I tried to create a shipping zone under WooCommerce>Settings>

Screenshot

Did I miss something in the code or?. But in my checkout page I can the entries correctly, however no shipping method is selected since I can't create a shipping zone under settings.

Thanks in advance for your help.

@imasimali
Copy link

Client asked me to add Lahore to States

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );

function custom_woocommerce_states( $states ) {

$states['PK'] = array(
'Lahore' => 'Lahore',
'Azad Kashmir' => 'Azad Kashmir',
'Balochistan' => 'Balochistan',
'FATA' => 'FATA',
'Gilgit Baltistan' => 'Gilgit Baltistan',
'Islamabad Capital Territory' => 'Islamabad Capital Territory',
'Khyber Pakhtunkhwa' => 'Khyber Pakhtunkhwa',
'Punjab' => 'Punjab',
'Sindh' => 'Sindh',
);

return $states;
}

@Etokai
Copy link

Etokai commented Mar 15, 2024

Can someone please tell me how to use this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment