Woocommerce Readonly Billing Fields

前端 未结 4 1463
暖寄归人
暖寄归人 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条回答
  • 2021-01-07 12:56
    $checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
    

    this solves the problem

    0 讨论(0)
  • 2021-01-07 13:02

    This one worked for me. https://www.sitekickr.com/snippets/woocommerce/make-checkout-field-read

    add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
    function my_woocommerce_billing_fields($fields)
    {
       $fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
       $fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
            
       return $fields;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-07 13:06

    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;
    }
    
    0 讨论(0)
提交回复
热议问题