【发布时间】:2021-11-22 04:30:17
【问题描述】:
基于How to remove all Woocommerce checkout billing fields without errors 答案代码,这是我的代码尝试:
add_action( 'woocommerce_before_checkout_form', 'hide_checkout_billing_country', 5 );
function hide_checkout_billing_country() {
echo '<style>#billing_state_field{display:none !important;}</style>';
}
add_filter('woocommerce_billing_fields', 'customize_checkout_fields', 100 );
function customize_checkout_fields( $fields ) {
if ( is_checkout() ) {
// HERE set the required key fields below
$chosen_fields = array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'postcode', 'country', 'state');
foreach( $chosen_fields as $key ) {
if( isset($fields['billing_'.$key]) && $key !== 'state') {
unset($fields['billing_'.$key]); // Remove all define fields except country
}
}
}
return $fields;
}
我的目标是根据国家/地区隐藏状态字段。例如。
- 如果 country = Italy 则显示 state 字段
- 如果 country = France 则隐藏 state 字段..
所以我尝试插入一个 if 条件,但没有得到想要的结果。
如果有人能给我建议,将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce checkout