ahmadwp
ahmadwp

Reputation: 348

How to make Woocommerce checkout page fields to read-only

For edit woocommerce (WordPressss plugin) account page fields and make to read-only, easy edit woocommerce/templates/myaccount/form-edit-account.php file and only add read-only to end of fields tags end (sorry my English and programming is not professional and good)

But for edit woocommerce checkout page fields and make one, or two and more fields, for example, email field, to read-only, cannot do the previous way

Please help me to do it, I have not idea.

It would be much better if, teach me to do it from functions.php easy and better and durable way (if it is possible).

thank you again

Upvotes: 1

Views: 2540

Answers (2)

Amritosh pandey
Amritosh pandey

Reputation: 404

You can use the following code in your theme's "function.php" file.

  add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
        function customization_readonly_billing_fields($checkout_fields){
            $current_user = wp_get_current_user();;
            $user_id = $current_user->ID;
            foreach ( $checkout_fields['billing'] as $key => $field ){
                if($key == 'billing_address_1' || $key == 'billing_address_2'){
                    $key_value = get_user_meta($user_id, $key, true);
                    if( strlen($key_value)>0){
                        $checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
                    }
                }
            }
            return $checkout_fields;
        }

This function will check if the address fields have value (i.e. if the address is specified), and if it has value, makes the field/s readonly. Else keeps the fields open to add data for the user. You can apply the same concept for each required fields.

Hope this helps.

Thank You!

Upvotes: 1

ahmadwp
ahmadwp

Reputation: 348

very thank you for answer mr amritosh pandey this code work correctly

for only email field , remove:

if($key == 'billing_address_1' || $key == 'billing_address_2'){

and i change to

if($key == 'billing_email'){

and work perfect and correctly

my new queston is > if use for two field , example: mobile (tel) and email or more field Should i repeat all codes? from add_action to return $checkout_fields; }

or can repeat if($key line 2 or 3 or more?

example:

...
...
if($key == 'billing_email'){
if($key == 'billing_phone'){
if($key == 'billing_postcode'){
...
...

thank you

Upvotes: 0

Related Questions