Search code examples
phpwordpresswoocommercereadonlyaccount

Disable or make read only the fields from edit account pages in Woocommerce


Currently the Edit Account Details page have these fields: First Name, Last Name, Email address, Current Password & New Password.

Now I need to disable the First Name, Last Name, Email address fields for customers user only. I am using Flatsome WP theme and Woocommerce plugins.

How can I do this?


Solution

  • Update

    For edit "Account details" fields (email field), you will need to edit myaccount/form-edit-account.php template file as those fields are hard coded in the template

    You will have to add a readonly attribute to the related input fields like in this extract example:

    <p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
    
        <label for="account_first_name"><?php esc_html_e( 'First name', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_first_name" id="account_first_name" autocomplete="given-name" value="<?php echo esc_attr( $user->first_name ); ?>" readonly />
    </p>
    

    Official documentation: Overriding templates via a theme

    For My account > edit billing address, the following code will make readonly the billing fields first name, last name and email:

    add_filter( 'woocommerce_billing_fields', 'readonly_billing_account_fields', 25, 1 );
    function readonly_billing_account_fields ( $billing_fields ) {
        // Only my account billing address for logged in users
        if( is_account_page() ){
    
            $readonly = ['readonly' => 'readonly'];
    
            $billing_fields['billing_first_name']['custom_attributes'] = $readonly;
            $billing_fields['billing_last_name']['custom_attributes'] = $readonly;
            $billing_fields['billing_email']['custom_attributes'] = $readonly;
        }
        return $billing_fields;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Note: To enable that on checkout page too, Just remove if( is_account_page() ){ and the closing bracket } before return $billing_fields;.


    To remove those 3 fields you will use:

    add_filter( 'woocommerce_billing_fields', 'remove_billing_account_fields', 25, 1 );
    function remove_billing_account_fields ( $billing_fields ) {
        // Only my account billing address for logged in users
        if( is_account_page() ){
            unset($billing_fields['billing_first_name']);
            unset($billing_fields['billing_last_name']);
            unset($billing_fields['billing_email']);
        }
        return $billing_fields;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.