I've created a woo-commerce checkout field with wooocmmerec checkout field editor plugin like the below image that is supposed to appear conditionally at the checkout and here is the code that enables it.
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' );
This code works flawlessly at the checkout but the problem is when a user tries to edit their billing address at my account then they see those custom fields and are not able to edit their address. I don't want to display nor save the custom fields at all to the customers at their my account edit billing address form. I tried the below code to unset those custom fields at the billing address in my account, I am able to hide the fields but am not able to save them as the tax id field is required at the checkout. I want the tax id to be required at the checkout and I don't want to have any custom fields at the edit billing address in my account? Any insights on how to achieve that?
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 );
To hide custom fields from my account > Edit billing address, you will use the following:
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;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: For shipping fields you will use the hook woocommerce_shipping_fields
instead.
Related:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.