Make state checkout field required for a specific country in Woocommerce

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 05:23:39

问题


The country of Vietnam in Woocommerce does not have the states, so I added some states to my checkout page.

This is my code:

add_filter( 'woocommerce_states', 'vietnam_cities_woocommerce' );
   function vietnam_cities_woocommerce( $states ) {
     $states['VN'] = array(
      'HCM' => __('Hồ Chí Minh', 'woocommerce') ,
      'HANOI' => __('Hà Nội', 'woocommerce') ,
      'HAIPHONG' => __('Hải Phòng', 'woocommerce') ,
           );
      return $states;
}

It do work as I would like, but it is an optional field for Vietnam.

How to make this state field as required for Vietnam?

Any help is appreciated.


回答1:


The following function will make for Vietnam the state field as a required in woocommerce:

add_filter( 'woocommerce_get_country_locale', 'custom_country_locale', 10, 1 );
function custom_default_address_fields( $locale ) {
    $locale['VN']['state']['required'] = true;

    return $locale;
}

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



来源:https://stackoverflow.com/questions/53137583/make-state-checkout-field-required-for-a-specific-country-in-woocommerce

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