I use this code to create custom checkout fields and reorder fields:
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;
}
The conditional mechanism:
- By default, the
billing_status
dropdown selector will be onIndividual
option value and will show onlybilling_serial
custom field. - When the
billing_status
dropdown selector will switched to"Company"
** option value , 2 more fields will appear:billing_company
existing field (beforebilling_serial
)billing_number_id
custom fields (at the end)
How can I achieve this?
Thanks
Reference: WooCommerce - Checkout conditional fields for different persons custom status
For WooCommerce 3+ (update):
Below I am just updating the part related to ordering fields:
Reference: Reordering checkout fields in WooCommerce 3
Original answer:
Here is a solution with a jQuery/JS script to make working the conditional mechanism just as you expect… The php code is completed with some necessaries functions…
PHP code (goes in function.php):
Javascript
cfields.js
code (external file):All this code has been tested and works