亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何禁用 WooCommerce 結帳中預填的字段?

如何禁用 WooCommerce 結帳中預填的字段?

PHP
慕村225694 2023-05-26 16:11:40
我想阻止(例如,將這些字段設置為只讀)用戶更改他們在 WooCommerce 結帳表單上的賬單信息。我目前正在使用這個代碼片段: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;}但問題是:如果用戶在注冊時沒有填寫這些字段中的任何一個,他將無法在結帳表單中插入他的數據,因為這些字段不可編輯。我的問題是: 如果字段不為空,如何使它們只讀(或禁用)誰能幫我解決這個問題?
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

但是從 WooCommerce 3 開始,您可以使用如下WC_Checkout?get_value()專用方法:

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;

}

代碼進入您的活動子主題(或活動主題)的 functions.php 文件。測試和工作。


如果您希望此代碼在“我的帳戶”>“地址”>“編輯...”中也處于活動狀態,您只需is_checkout() &&從第一個IF語句中刪除即可。


查看完整回答
反對 回復 2023-05-26
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

我相信這就是您要找的,評論并在代碼中添加解釋


function mycustom_woocommerce_billing_fields( $fields ) {   

    // Get current user

    $user = wp_get_current_user();


    // User id

    $user_id = $user->ID;


    // User id is found

    if ( $user_id > 0 ) { 

        // Fields

        $read_only_fields = array ( 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone' );


        // Loop

        foreach ( $fields as $key => $field ) {     

            if( in_array( $key, $read_only_fields ) ) {

                // Get key value

                $key_value = get_user_meta($user_id, $key, true);


                if( strlen( $key_value ) > 0 ) {

                    $fields[$key]['custom_attributes'] = array(

                        'readonly'=>'readonly'

                    );

                }

            }

        }

    }


   return $fields;

}

add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );



查看完整回答
反對 回復 2023-05-26
  • 2 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號