Based on this working answer:
Custom dropdown selector showing or hiding other Checkout custom fields
In WooCommerce checkout page I use the code below to create some additional custom fields and reorder all the checkout fields. I use a jQuery script to show/hide some fields based on a selector choice.
Here is my new code:
// Registering external jQuery/JS file
function cfields_scripts() {
/* IMPORTANT NOTE: For a child theme replace get_template_directory_uri() by get_stylesheet_directory_uri()
The external cfields.js file goes in a subfolder "js" of your active child theme or theme.*/
wp_enqueue_script( 'checkout_script', get_template_directory_uri().'/js/cfields.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'cfields_scripts' );
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'] = __('Statut Juridic', 'my_theme_slug');
$fields['billing']['billing_status']['placeholder'] = __('Alege statutul', 'my_theme_slug');
$fields['billing']['billing_status']['options'] = array(
'1' => __( 'Persoana Fizica', '' ),
'2' => __( 'Persoana Juridica', '' )
);
// Customizing 'billing_company' field ['required']
$fields['billing']['billing_company']['required'] = false;
$fields['billing']['billing_company']['class'] = array('form-row-wide', 'status-group2');
// The "Nr. registrul comertului" text field
$fields['billing']['billing_ser_id']['type'] = 'text';
$fields['billing']['billing_ser_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_ser_id']['required'] = false;
$fields['billing']['billing_ser_id']['label'] = __('Nr. Reg. Comert', 'my_theme_slug');
$fields['billing']['billing_ser_id']['placeholder'] = __('Introdu numarul', 'my_theme_slug');
// The "Banca" text field
$fields['billing']['billing_bt_id']['type'] = 'text';
$fields['billing']['billing_bt_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_bt_id']['required'] = false;
$fields['billing']['billing_bt_id']['label'] = __('Banca', 'my_theme_slug');
$fields['billing']['billing_bt_id']['placeholder'] = __('Adauga Banca', 'my_theme_slug');
// The "IBAN" text field
$fields['billing']['billing_ib_id']['type'] = 'text';
$fields['billing']['billing_ib_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_ib_id']['required'] = false;
$fields['billing']['billing_ib_id']['label'] = __('IBAN', 'my_theme_slug');
$fields['billing']['billing_ib_id']['placeholder'] = __('Adauga IBAN-ul', 'my_theme_slug');
// The "CIF" text field
$fields['billing']['billing_cf_id']['type'] = 'text';
$fields['billing']['billing_cf_id']['class'] = array('form-row-wide', 'status-group2');
$fields['billing']['billing_cf_id']['required'] = false;
$fields['billing']['billing_cf_id']['label'] = __('Cod Fiscal', 'my_theme_slug');
$fields['billing']['billing_cf_id']['placeholder'] = __('Adauga CIF-ul', 'my_theme_slug');
// 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_ser_id', 'billing_bt_id',
'billing_ib_id', 'billing_cf_id'
);
foreach($fields_order as $field) $ordered_fields[$field] = $fields['billing'][$field];
$fields['billing'] = $ordered_fields;
// Returning Checkout customized billing fields
return $fields;
}
// Process the checkout
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_ser_id'] )
wc_add_notice( __( 'Please enter your Serial id.' , 'my_theme_slug' ), 'error' );
if ( ! $_POST['billing_bt_id'] )
wc_add_notice( __( 'Please enter your Serial id.' , 'my_theme_slug' ), 'error' );
if ( ! $_POST['billing_ib_id'] )
wc_add_notice( __( 'Please enter your Serial id.' , 'my_theme_slug' ), 'error' );
if ( ! $_POST['billing_cf_id'] )
wc_add_notice( __( 'Please enter your Serial id.' , 'my_theme_slug' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['billing_ser_id'] ) )
update_post_meta( $order_id, 'billing_ser_id', sanitize_text_field( $_POST['billing_ser_id'] ) );
if ( ! empty( $_POST['billing_bt_id'] ) )
update_post_meta( $order_id, 'billing_bt_id', sanitize_text_field( $_POST['billing_bt_id'] ) );
if ( ! empty( $_POST['billing_ib_id'] ) )
update_post_meta( $order_id, 'billing_ib_id', sanitize_text_field( $_POST['billing_ib_id'] ) );
if ( ! empty( $_POST['billing_cf_id'] ) )
update_post_meta( $order_id, 'billing_cf_id', sanitize_text_field( $_POST['billing_cf_id'] ) );
}
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('My serial identification').':</strong> ' . get_post_meta( $order->id, 'billing_ser_id', true ) . '</p>';
echo '<p><strong>'.__('My serial identification').':</strong> ' . get_post_meta( $order->id, 'billing_bt_id', true ) . '</p>';
echo '<p><strong>'.__('My serial identification').':</strong> ' . get_post_meta( $order->id, 'billing_ib_id', true ) . '</p>';
echo '<p><strong>'.__('My serial identification').':</strong> ' . get_post_meta( $order->id, 'billing_cf_id', true ) . '</p>';
}
Javascript cfields.js
code (incomplete external file):
// This file named "cfields.js" goes in a subfolder "js" of your active child theme or theme
jQuery(document).ready(function($){
$('#billing_company_field').hide(function(){
$(this).removeClass("validate-required");
});
$('#billing_ser_id_field').hide(function(){
$(this).removeClass("validate-required");
});
$("#billing_number_id_field").addClass("validate-required");
$("#billing_status").change(function(){
if($("#billing_status option:selected").val() == "2"){
$('#billing_company_field').show(function(){
$(this).addClass("validate-required");
});
$('#billing_ser_id_field').show(function(){
$(this).addClass("validate-required");
});
} else if($("#billing_status option:selected").val() == "1"){
$('#billing_company_field').hide(function(){
$(this).removeClass("validate-required");
});
$('#billing_ser_id_field').hide(function(){
$(this).removeClass("validate-required");
});
}
});
});
As I have some additional fields and what I need now is when the billing_status
selector is on:
- Persoana Fizica option value (Individual): showing only
billing_serial
custom field. Persoana Juridica option value (Company), 4 more fields will appear:
billing_company
existing field (at first, beforebilling_serial
)billing_registration_id
custom field (this field is always shown, in both cases)billing_bank_id
custom fieldbilling_bankno_id
custom fieldbilling_cif_id
custom field
Also I would like to display this data on Thankyou Oder receive page and on email notifications.
I haven't find the way to get it working. How can I make it work properly?
Thanks in advance.
As you have add others customs fields and make some changes, you will find below the necessary code to make it work properly. This is becoming a real development and shouldn't be asked in here. I try to always finish what I have began, so I answer it.
Here is the PHP code (which goes in function.php):
Javascript
cfields.js
code (external file):All this code has been tested and works