根据所选国家/地区,不需要 woocommerce 结帐表单状态字段 [英] Making woocommerce checkout form state field not required based on selected country

查看:24
本文介绍了根据所选国家/地区,不需要 woocommerce 结帐表单状态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WordPress+Woo 商务网站,我想根据所选国家/地区将结帐表单上的州字段设为可选.例如,我希望我的客户仅在属于美国州之一时才添加州字段.

I have a WordPress+Woo-commerce site and I want to make the state field on the checkout form optional based on the selected country. For example I want my customers to add state field only if they belong to one of U.S.A. states.

我该怎么做?

推荐答案

add_filter( 'woocommerce_default_address_fields' , 'custom_override_state_required' );
function custom_override_state_required( $address_fields ) {
  $wc = WC();
  $country = $wc->customer->get_country();
  if($country !== 'US'){
     $address_fields['state']['required'] = false;
  }
  return $address_fields;
}

这是对我有用的代码.我在没有编辑@zipkundan 答案的情况下发布了这个答案,因为我在他的答案中没有看到任何错误,但不知何故它对我不起作用.那可能是因为 WooCommerce 版本或可能有其他原因!!如果有人有类似的要求,我建议您尝试两种答案.

This is code that worked for me. I am posting this answer without editing @zipkundan answer because I don't see any mistakes in his answer but somehow it was not working for me. That may be because of WooCommerce version or may have another reason!! I recommend to try both answers if someone have similar requirements.

这篇关于根据所选国家/地区,不需要 woocommerce 结帐表单状态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆