问题描述
我设法在WooCommerce网上商店中禁用了每个结帐字段,但由于某些奇怪的原因,只有“ billing_state”不起作用。
这些是我正在使用的钩子
add_filter( 'woocommerce_checkout_fields','custom_checkout_fields' );
function custom_checkout_fields( $fields ) {
$fields['billing']['billing_postcode']['custom_attributes']['disabled'] = 'disabled';
$fields['billing']['billing_city']['custom_attributes']['disabled'] = 'disabled';
$fields['billing']['billing_state']['custom_attributes']['disabled'] = 'disabled';
return $fields;
}
我也已经尝试过billing_country字段,因为这也是一个选择字段,也许与此有关,但是在billing_country字段上,代码可以正常工作。
任何人都知道为什么此功能对我在de checkout中的billing_state字段不起作用?也许与条件逻辑有关,因为在您可以选择的所有国家中,州并不总是可见的?
解决方法
在禁用之前,请尝试这样并设置默认国家/地区。您不能禁用没有任何值的必填字段。您还应该为下拉菜单设置国家/地区,有关更多信息,您可以点击链接Make checkout country dropdown readonly in Woocommerce
add_filter('woocommerce_checkout_fields','readdonly_country_select_field');
function readdonly_country_select_field( $fields ) {
// Set billing and shipping state to AU
WC()->customer->set_billing_state('state');
// Make billing and shipping country field read only
$fields['billing']['billing_state']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}`