我用這個代碼來創建自定義結賬字段和重新排序字段:自定義下拉選擇顯示或隱藏其他結帳的自定義字段
add_filter('woocommerce_checkout_fields', 'custom_checkout_billing_fields');
function custom_checkout_billing_fields($fields) {
// 1. Creating the additional custom billing fields
// The "status" selector
$fields['billing']['billing_status']['type'] = 'select';
$fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');
$fields['billing']['billing_status']['required'] = true;
$fields['billing']['billing_status']['label'] = __('User status', 'my_theme_slug');
$fields['billing']['billing_status']['placeholder'] = __('Chose an option', 'my_theme_slug');
$fields['billing']['billing_status']['options'] = array(
'' => 'Chose an option',
'1' => 'Legal entity',
'2' => 'Individual'
);
// The "Serial ID" text field
$fields['billing']['billing_number_id']['type'] = 'text';
$fields['billing']['billing_number_id']['class'] = array('form-row-wide', 'status-group1');
$fields['billing']['billing_number_id']['required'] = true;
$fields['billing']['billing_number_id']['label'] = __('Serial ID', 'my_theme_slug');
$fields['billing']['billing_number_id']['placeholder'] = __('Enter your Serial ID', 'my_theme_slug');
// Customizing 'billing_company' field ['required']
$fields['billing']['billing_company']['required'] = false;
// The "Serial ID" text field
$fields['billing']['billing_serial']['type'] = 'text';
$fields['billing']['billing_serial']['class'] = array('form-row-wide', 'status-group1');
$fields['billing']['billing_serial']['required'] = false;
$fields['billing']['billing_serial']['label'] = __('Serial ID', 'my_theme_slug');
$fields['billing']['billing_serial']['placeholder'] = __('Enter your Serial ID', 'my_theme_slug');
// 2. Customizing 'billing_email' and 'billing_phone' fields ['class']
$fields['billing']['billing_email']['class'] = array('form-row-first', 'status-group1');
$fields['billing']['billing_phone']['class'] = array('form-row-last', 'status-group1');
// 3. Ordering the billing fields
$fields_order = array(
'billing_first_name', 'billing_last_name', 'billing_email',
'billing_phone', 'billing_address_1', 'billing_address_2',
'billing_postcode', 'billing_city', 'billing_country',
'billing_status',
'billing_company', 'billing_number_id', 'billing_serial'
);
foreach($fields_order as $field) $ordered_fields[$field] = $fields['billing'][$field];
$fields['billing'] = $ordered_fields;
// Returning Checkout customized billing fields
return $fields;
}
有條件的機制:
- 默認情況下,
billing_status
下拉選擇器將在Individual
期權價值和將顯示只有billing_serial
自定義字段。 - 當
billing_status
下拉選擇將切換到"Company"
**的選項值,2個更多的字段將出現:billing_company
現有場(前billing_serial
)billing_number_id
自定義字段(在結束)
我該如何做到這一點?
由於
參考:WooCommerce - Checkout conditional fields for different persons custom status
這很簡單,用戶將完成唯一的名字,姓氏,在此之後,如果他是法人或個人,他將選擇他的身份。 如果他將從下拉選擇器菜單中選擇個人狀態,則需要顯示的字段將爲電話號碼,地址,電子郵件地址和序列號。 如果他是法人,我需要以下字段: - 電話 - 電子郵件 - 地址 - 和3個其他領域 –
好吧,我想這種結構的原因 - 爲公司 序列ID號如果我想根據人的類型將這些數據納入發票。現在有道理? –
將數據帶到謝謝和發票將是未來的其他問題,我認爲:) – LoicTheAztec