read only wocommerce checkout field code example

Example: read only wocommerce checkout field

/**
 * Billing Read Only File WITH USER LOGIN/ID...
 *
 * This is for make check out Feilds READ ONLY.
 * 
 * You can make it read only.
 */

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;
}




/**
 * Billing Read Only File
 *
 * This is for make check out Feilds READ ONLY.
 * 
 * You can make it read only.
 */

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
  $readonly = ['readonly' => 'readonly'];
  $billing_fields['billing_address_2']['custom_attributes'] = $readonly;
  return $billing_fields;
}

Tags:

Misc Example