[英]Hide custom checkout fields from appearing in WooCommerce my account edit address
我已经使用 wooocmmerec 结帐字段编辑器插件创建了一个 woo-commerce 结帐字段,如下图所示 这应该有条件地出现在结帐时,这是启用它的代码。
function ga_checkout_fields($fields) {
global $user_country;
//$user_country = 'in'; //for test in dev
if( $user_country != 'in') { //hide gst if the location is not india
unset($fields['billing']['billing_gst_number']);
}
if( $user_country != 'br') { //hide taxid if the location is not brazil
unset($fields['billing']['billing_br_tax_id']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ga_checkout_fields' );
此代码在结帐时完美运行,但问题是当用户尝试在我的帐户中编辑他们的帐单地址时,他们会看到这些自定义字段并且无法编辑他们的地址。 我不想在我的帐户编辑帐单地址表单中向客户显示或保存自定义字段。 我尝试使用以下代码在我的帐户中的帐单地址取消设置这些自定义字段,我可以隐藏这些字段但无法保存它们,因为结帐时需要税号字段。 我希望在结帐时需要税号,并且我不想在我的帐户中的编辑帐单地址中有任何自定义字段? 关于如何实现这一目标的任何见解?
function ga_remove_the_address_field( $address, $load_address ) {//this removes fields in the edit form
global $user_country;
if( $user_country != 'in') { //hide gst if the location is not india
unset($address['billing_gst_number']);
}
if( $user_country != 'br') { //hide taxid if the location is not brazil
unset($address['billing_br_tax_id']);
}
return $address;
}
add_filter( 'woocommerce_address_to_edit', 'ga_remove_the_address_field', 10, 2 );
要从我的帐户中隐藏自定义字段 > 编辑帐单地址,您将使用以下内容:
add_filter( 'woocommerce_billing_fields', 'ga_remove_account_custom_fields', 1000 );
function ga_remove_account_custom_fields( $fields ) {
// Only on My account > Edit address section
if ( is_wc_endpoint_url('edit-address') ) {
// Here define the custom field Ids to hide in the array
$field_ids = array('billing_gst_number', 'billing_br_tax_id');
foreach ( $field_ids as $field_id ) {
if( isset($fields[$field_id]) ) {
unset($fields[$field_id]);
}
}
}
return $fields;
}
代码进入活动子主题(或活动主题)的functions.php文件。 测试和工作。
注意:对于运输字段,您将使用钩子woocommerce_shipping_fields
代替。
有关的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.