Add field to billing form in WooCommerce checkout page add filter for woocommerce_billing_fields
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing_options'] = [
'label' => __('NIP', 'woocommerce'), // Add custom field label
'placeholder' => _x('Company NIP/VAT number', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('input-vat-css') // add class name
];
return $fields;
}
Display information in admin panel in order tab:
<?php
add_action('woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1);
function my_custom_checkout_field_display_admin_order_meta($order)
{
echo '<p><strong>' . __('Custom field title here') . ':</strong> ' . get_post_meta($order->get_id(), '_shipping_custom_field_here', true) . '</p>'; // "NIP"
}
We could also use woocommerce_checkout_fields hook instead of woocommerce_billing_fields but then we would also need to access the array in different way – like so:
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing']['billing_options'] = [ // <<<<<<<<<<<<<<
'label' => __('NIP', 'woocommerce'), // Add custom field label
'placeholder' => _x('Company NIP/VAT number', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
];
return $fields;
}
Read also : WP – Adding custom field to Woocommerce checkout page
https://stackoverflow.com/questions/42341548/add-a-custom-field-to-woocommerce-billing-form
https://gist.github.com/mikejolley/1860056
https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/