我有条件地在woocommerce结帐页面上取消设置帐单邮寄地址字段,但是在提交下订单时,woocommerce显示错误:
请输入一个地址以继续。
然后,我尝试在woocommerce_default_address_fields上添加过滤器以使字段可选-仅在未设置字段的情况下,这似乎才起作用。
// make address fields optional - this works fine without the next filter
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $address_fields;
// All field keys in this array
$fields = array('country','company','address_1','address_2','city','state','postcode');
// Loop through each address fields (billing and shipping)
foreach( $fields as $key_field )
$address_fields[$key_field]['required'] = false;
return $address_fields;
}
//conditionally unset fields
add_filter( 'woocommerce_checkout_fields' , 'simplify_checkout' );
function simplify_checkout( $fields ) {
$customField = false;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if cart item has attribute
if ( ! empty ($cart_item['custom_attribute']) ) $registry = true;
}
if( $customField ) {
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
}
return $fields;
}
我希望仍然能够有条件地隐藏/取消字段设置以及成功提交结帐。
答案 0 :(得分:1)
两天前,我在验证消息中遇到了这个问题。我只卖给一个国家,所以我创建了以下解决方法。也许如果您出售的国家较少,这也可能对您有用:
unset($fields['billing']['billing_country']);
现在,由于“国家/地区”字段为 强制性的。您现在可以使用CSS删除它。
#billing_country_field {
display: none;
}