[英]Make the Checkout Billing address read only in WooCommerce
我們有一個網站,用於提供然后交付的訂單。 現在的問題是我們想要利用 My Account Address Billing 中的詳細信息作為發貨地址,在結帳時我們希望從那里填充它,但不允許用戶在該結帳頁面 Billing form 上進行任何更改。
這段代碼:
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;
}
僅使地址部分不被更改,但可以修改名字、姓氏、手機等其他所有內容。
我們需要鎖定所有這些字段,就像將地址字段鎖定為只讀的代碼一樣。
您可以刪除if($key == 'billing_address_1' || $key == 'billing_address_2'){
這一行的所有字段。 檢查 beloe 代碼。
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 ){
$key_value = get_user_meta($user_id, $key, true);
if( $key_value != '' ){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
return $checkout_fields;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.