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ǔ)句中刪除即可。

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 );
- 2 回答
- 0 關(guān)注
- 157 瀏覽
添加回答
舉報(bào)