am目前在woocommerce中遇到billing_state字段问题。 我的想法是将此字段的类型更改为“选择”(通过简单的文本输入) 但是,即使在应用了官方文档中的“ woocommerce_checkout_fields”或“ woocommerce_default_address_fields”过滤器之后 ,由于仍无法显示简单的输入,因此无法获得所需的结果,我尝试了另一个字段(billing_city),并且效果很好,因此我var_dumped了fields数组以查看有何区别,我发现state字段有一个验证名为(state),但city字段不是==>,因此我尝试使用“ unset($ fields ['billing'] ['billing_state'] ['validate']);”删除验证但是它仍然不起作用! 这是我的代码:
add_filter( 'woocommerce_checkout_fields', 'no_state_validation' );
function no_state_validation( $fields ){
unset( $fields['billing']['billing_state']['validate'] );
return $fields;
}
add_filter( 'woocommerce_default_address_fields' , 'customize_checkout_city_field' );
function customize_checkout_city_field( $address_fields ) {
// Set HERE the cities (one line by city)
$towns_cities_arr = array(
'0' => __('Select your city', 'my_theme_slug'),
'paris' => 'Paris',
'versailles' => 'Versailles',
'cannes' => 'Cannes',
);
// Customizing 'billing_city' field
$address_fields['city']['type'] = 'select';
$address_fields['city']['class'] = array('form-row-last', 'my-custom-class'); // your class here
$address_fields['city']['label'] = __('Town / city', 'my_theme_slug');
$address_fields['city']['options'] = $towns_cities_arr;
// $address_fields['state']['type'] = 'select';
// $address_fields['state']['class'] = array('form-row-last', 'my-custom-class');
// $address_fields['state']['label'] = __('Wilaya ', 'my_theme_slug');
// $address_fields['state']['options'] = $towns_cities_arr;
// $address_fields['state']['validate'] = false;
$address_fields['address_1']['type'] = 'select';
$address_fields['address_1']['class'] = array('form-row-last', 'my-custom-class');
$address_fields['address_1']['label'] = __('Wilaya ', 'my_theme_slug');
$address_fields['address_1']['options'] = $towns_cities_arr;
$address_fields['state']['type'] = 'select';
$address_fields['state']['class'] = array('form-row-last', 'my-custom-class');
$address_fields['state']['label'] = __('Wilaya ', 'my_theme_slug');
$address_fields['state']['options'] = $towns_cities_arr;
//echo '<script>console.log('.var_dump($address_fields).')</script>';
// Returning Checkout customized fields
return $address_fields;
}
我将很感谢您的帮助 here are the results of my code in the checkout interface for woocommerce