第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何禁用 WooCommerce 結(jié)帳中預(yù)填的字段?

如何禁用 WooCommerce 結(jié)帳中預(yù)填的字段?

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

2 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

但是從 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;

}

代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件。測(cè)試和工作。


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


查看完整回答
反對(duì) 回復(fù) 2023-05-26
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

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


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 );



查看完整回答
反對(duì) 回復(fù) 2023-05-26
  • 2 回答
  • 0 關(guān)注
  • 157 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)