Last active
April 22, 2024 12:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
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>
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.
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;
}
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
Bro You are really amazing. You solved my issue. Thanks a lot