如何禁用WooCommerce结帐中预填写的字段? [英] How to disable fields that are pre-filled in WooCommerce checkout?

查看:56
本文介绍了如何禁用WooCommerce结帐中预填写的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止(例如,使这些字段为只读的)用户在WooCommerce结帐表单上更改其帐单信息.

我当前正在使用以下代码段:

 <代码> add_filter('woocommerce_billing_fields','mycustom_woocommerce_billing_fields',10,1);函数mycustom_woocommerce_billing_fields($ fields){$ fields ['billing_first_name'] ['custom_attributes'] = array('readonly'=>'readonly');$ fields ['billing_last_name'] ['custom_attributes'] = array('readonly'=>'readonly');$ fields ['billing_email'] ['custom_attributes'] = array('readonly'=>'readonly');$ fields ['billing_phone'] ['custom_attributes'] = array('readonly'=>'readonly');返回$ fields;} 

但是问题是::如果用户未在注册中填写任何这些字段,则由于这些字段不可编辑,因此无法将其数据插入到结帐表格中.>

我的问题是:如果字段不为空,如何使它们成为只读(或禁用)

有人可以帮助我吗?

解决方案

7uc1f3r的答案当然可以获取用户数据……但是自WooCommerce 3以来,您可以使用地址">编辑"中也处于活动状态,则只需从第一个 中删除 is_checkout()&& IF 语句.

I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.

I'm currently using this code snippet:

add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
   $fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
   $fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
   $fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
   $fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
   return $fields;
}

But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.

My question is: If the fields are not empty, how to make them readonly (or disabled)

Someone who can help me with this?

解决方案

The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout get_value() dedicated method as follow:

add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
    // On checkout and if user is logged in
    if ( is_checkout() && is_user_logged_in() ) {
        // Define your key fields below
        $keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];

        // Loop through your specific defined fields
        foreach ( $keys_fields as $key ) {
            // Check that a value exist for the current field
            if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
                // Make it readonly if a value exist
                $fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
            }
        }
    }
    return $fields;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() && from the first IF statement.

这篇关于如何禁用WooCommerce结帐中预填写的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆