我們有一個網站,用于提供然后交付的訂單。現在的問題是我們想要使用 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;
}
僅使地址部分不被更改,但可以修改名字、姓氏、手機等其他所有內容。
我們需要鎖定所有這些欄位,就像將地址欄位鎖定為只讀的代碼一樣。
uj5u.com熱心網友回復:
您可以洗掉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 != '' ){
if( $key == 'billing_country' || $key == 'billing_state' || $key == 'billing_suburb' ){
$checkout_fields['billing'][$key]['custom_attributes'] = array('disabled'=>'disabled');
}else{
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
對于billing_country,billing_state和billing_suburb,您必須在 hidden 中傳遞值,因為選擇下拉串列已禁用,單擊放置訂單時不會傳遞選項值,為了解決此問題,我們可以使用我們的值添加隱藏欄位。
add_action('woocommerce_after_order_notes', 'billing_countryand_state_hidden_field');
function billing_countryand_state_hidden_field($checkout){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
echo '<input type="hidden" name="billing_country" value="'.get_user_meta($user_id, 'billing_country', true).'">';
echo '<input type="hidden" name="billing_state" value="'.get_user_meta($user_id, 'billing_state', true).'">';
echo '<input type="hidden" name="billing_suburb" value="'.get_user_meta($user_id, 'billing_suburb', true).'">';
}
測驗和作業

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337092.html
標籤:php WordPress的 求购 查看 计费
