Woocommerce Readonly Billing Fields

前端 未结 4 1491
暖寄归人
暖寄归人 2021-01-07 12:42

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 avoi

4条回答
  •  萌比男神i
    2021-01-07 13:06

    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.

提交回复
热议问题