Woocommerce Readonly Billing Fields

2020-03-08 05:22发布

问题:

I have some e-commerce website where the customer billing address is predefined on the back-end.

I need to set the "Billing Address" fields as 'readonly' to avoid the customer to replace the information placed there... but i don´t know how/where to do it...

Is it possible?

回答1:

Put 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 checks 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 user. Hope this helps.



回答2:

$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');

this solves the problem



回答3:

You have not specified which form you want to customize making the billing address fields read-only. Normally the billing address fields appear on two types of forms on a WooCommerce site:

  1. On a checkout form
  2. On a my-account/edit-address/billing/ page

If your case is the first one, then zipkundan's answer is the best one. But if your case is the second one, then copy and paste the following code to your active theme's (or child theme if any) functions.php file:

add_filter('woocommerce_address_to_edit', 'cb_woocommerce_address_to_edit');
function cb_woocommerce_address_to_edit($address){
        array_key_exists('billing_first_name', $address)?$address['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly'):'';
        array_key_exists('billing_last_name', $address)?$address['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly'):'';
        array_key_exists('billing_email', $address)?$address['billing_email']['custom_attributes'] = array('readonly'=>'readonly'):'';
        array_key_exists('billing_email-2', $address)?$address['billing_email-2']['custom_attributes'] = array('readonly'=>'readonly'):'';
        return $address;
}

The above code will make the following fields read-only:

  • Billing first name
  • Billing last name
  • Billing email address
  • Billing confirm email address

Array keys for other form fields on the same page are as follows:

  • billing_company
  • billing_country
  • billing_address_1
  • billing_address_2
  • billing_city
  • billing_state
  • billing_postcode
  • billing_phone

Additionally, you can make the read-only fields appear slightly faded out. So, add the following CSS to your theme's style.css

.woocommerce-address-fields input[readonly="readonly"]{
        opacity: 0.5;
}