Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Readonly Billing Fields

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?

like image 758
Jorge Avatar asked Oct 14 '25 17:10

Jorge


1 Answers

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.

like image 122
zipkundan Avatar answered Oct 18 '25 06:10

zipkundan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!