Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

php - Hide custom checkout fields from appearing in WooCommerce my account edit address

I've created a woo-commerce checkout field with wooocmmerec checkout field editor plugin like the below image enter image description here 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 );
question from:https://stackoverflow.com/questions/65621437/hide-custom-checkout-fields-from-appearing-in-woocommerce-my-account-edit-addres

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...