cheesyblast
cheesyblast

Reputation: 41

Add states for shipping zones instead of postcodes in Woocommerce

I have a woocommerce website. by default in woocommerce i can limit shipping zones by zipcode. But How can I add states of my country in shipping zones so that customers can choose the state they live by a "dropdown menu" in checkout page instead of entering zip code?

Upvotes: 2

Views: 2908

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253773

If states are not defined for your country in WooCommerce, you should need to defined them all with a letter code (2 or 3 uppercase characters) and the corresponding label name in an array.

Here below is an example based on France regions (Not defined in Woocommerce) for France country code 'FR' (so you need to set your woocommerce country code):

add_filter('woocommerce_states', 'add_custom_states_to_country');
add_filter('woocommerce_countries_allowed_country_states', 'add_custom_states_to_country');
function add_custom_states_to_country( $states ) {
    $states['FR'] = array(
        'AR' => __('Auvergne-Rhône-Alpes', 'woocommerce'),
        'BF' => __('Bourgogne-Franche-Comté', 'woocommerce'),
        'BR' => __('Bretagne', 'woocommerce'),
        'CV' => __('Centre-Val-de-Loire', 'woocommerce'),
        'CO' => __('Corse', 'woocommerce'),
        'IF' => __('Île-de-France', 'woocommerce'),
        'GE' => __('Grand Est', 'woocommerce'),
        'HF' => __('Hauts-de-France', 'woocommerce'),
        'NO' => __('Normandie', 'woocommerce'),
        'NA' => __('Nouvelle-Aquitaine', 'woocommerce'),
        'OC' => __('Occitanie', 'woocommerce'),
        'PL' => __('Pays de la Loire', 'woocommerce'),
        'PA' => __('Provence-Alpes-Côte d’Azur', 'woocommerce'),
    );
    return $states;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

The Country Iso Codes


In Shipping Zone Settings:

enter image description here

In Cart (shipping calculator):

enter image description here

In Checkout:

enter image description here

Upvotes: 7

Related Questions