Add states for shipping zones instead of postcodes in Woocommerce

北慕城南 提交于 2019-12-04 04:48:47

问题


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 ?

I'm new to coding, so can someone pls tell me where to enter the codes as well.

Please...


回答1:


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 bellow 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:

In Cart (shipping calculator):

In Checkout:



来源:https://stackoverflow.com/questions/51639599/add-states-for-shipping-zones-instead-of-postcodes-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!