Php 隐藏自定义签出字段,使其不显示在我的帐户编辑地址中
我已经用wooocmmerec checkout field editor插件创建了一个woo commerce checkout field,如下图所示,它应该有条件地出现在checkout中,下面是启用它的代码Php 隐藏自定义签出字段,使其不显示在我的帐户编辑地址中,php,wordpress,woocommerce,field,account,Php,Wordpress,Woocommerce,Field,Account,我已经用wooocmmerec checkout field editor插件创建了一个woo commerce checkout field,如下图所示,它应该有条件地出现在checkout中,下面是启用它的代码 function ga_checkout_fields($fields) { global $user_country; //$user_country = 'in'; //for test in dev if( $user_country != 'in')
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' );
这段代码在结帐时可以完美地工作,但问题是当用户试图在我的帐户上编辑他们的账单地址时,他们会看到这些自定义字段,并且无法编辑他们的地址。我根本不想在客户的“我的帐户编辑账单地址”表单中显示或保存自定义字段。
我尝试使用下面的代码取消设置我帐户中帐单地址的自定义字段,我可以隐藏这些字段,但无法保存它们,因为结帐时需要“税务id”字段。我希望结帐时需要税务id,并且我不希望在我的帐户中的编辑帐单地址处有任何自定义字段?对如何实现这一目标有何见解
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\u shipping\u字段
相关的: