-1

I am trying to disable my address 1 and address 2 fields on checkout page. I have used the below working code on my-account page, I tried to use the code after editing on checkout page to disable the address 1 and address 2 field. But unfortunately it doesn't work, Kindly help.

add_filter( 'woocommerce_checkout_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_user_logged_in() && is_account_page() ){

        $readonly = ['readonly' => 'readonly'];

        $billing_fields['address_1']['custom_attributes'] = $readonly;
        $billing_fields['address_2']['custom_attributes'] = $readonly;
    }
    return $billing_fields;
}
jeet singh
  • 71
  • 10
  • just remove completely your **`IF`** statement, so `if( is_user_logged_in() && is_account_page() ){`and the related closing bracket as the hook `woocommerce_checkout_fields` is only used on checkout pages, so no need of anything. – LoicTheAztec Jan 02 '21 at 09:22
  • Thanks for your comment, I tried that but it did not worked for me, anyways I got my answer :) – jeet singh Jan 02 '21 at 09:31
  • Sorry but this works… There is no need of an if statement as the hook `woocommerce_checkout_fields` is triggered only in checkout page. So the answer below is not correct. Now `$billing_fields['address_1']['custom_attributes']` need to be replaced with `$billing_fields['billing']['billing_address_1']['custom_attributes']` – LoicTheAztec Jan 02 '21 at 10:06
  • 1
    Now in your code `$billing_fields['address_1']['custom_attributes'] = $readonly;` need to be replaced with `$billing_fields['billing']['billing_address_1']['custom_attributes'] = $readonly;` and `$billing_fields['address_2']['custom_attributes'] = $readonly;` need to be replaced with `$billing_fields['billing']['billing_address_2']['custom_attributes'] = $readonly;` … and it will work without any IF statement. – LoicTheAztec Jan 02 '21 at 10:11

1 Answers1

2

Add is_checkout() on your condition then change the fields to billing_address_1 and 2.

add_filter( 'woocommerce_checkout_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_user_logged_in() && is_account_page()) || is_checkout()){

        $readonly = ['readonly' => 'readonly'];

        $billing_fields['billing']['billing_address_1']['custom_attributes'] = $readonly;
        $billing_fields['billing']['billing_address_2']['custom_attributes'] = $readonly;
    }
    return $billing_fields;
}
suii
  • 292
  • 2
  • 13