I’m new to PHP and WooCommerce customization, and I’ve attempted to implement a custom checkout field for collecting a Tax Exempt Number. While I have managed to create and display the field, I’m not sure if my implementation is complete. I’d appreciate any input or guidance on my setup and I apologize if I haven’t posted this correctly.
I’ve reviewed the following during my research:
- BusinessBloomer: WooCommerce: How to Add a Custom Checkout Field
- WooCommerce Customize Checkout Fields
- Stack Overflow: Load user custom data on WooCommerce admin new order custom fields
- Stack Overflow: Get and display the inputed data from custom Checkout fields in WooCommerce
What I tried:
- I used the
woocommerce_billing_fields
filter to add a new field for the Tax Exempt Number to the billing section of the checkout. - I utilized the
woocommerce_checkout_create_order
action to save the field data to both the order meta and the user meta. - Display in Admin Order Details:
- Included the field using the
woocommerce_admin_billing_fields
filter. - I ensured that the field data is loaded via AJAX (
woocommerce_ajax_get_customer_details
) when selecting a customer for a manual order.
- Allowed admin to correct field value using the
woocommerce_process_shop_order_meta
action. - I utilized the
woocommerce_order_details_after_customer_details
action to show the field in customer order details and thank you page (when logged in). Andwoocommerce_email_customer_details
to display the field in email notifications.
Here’s the complete code I have implemented so far:
/**
* WooCommerce: Custom Billing Checkout Field (Tax Exempt #)
*/
function custom_checkout_fields_settings() {
$text_domain = 'woocommerce';
return array(
'tax_exempt_number' => __('Tax Exempt #', $text_domain),
);
}
// Add Tax Exempt # Field to Checkout and My Account Edit Address
add_filter( 'woocommerce_billing_fields' , 'custom_billing_field' );
function custom_billing_field( $fields ) {
$fields['billing_tax_exempt_number'] = array(
'label' => __('Tax Exempt #', 'woocommerce'),
'placeholder' => _x('Enter Your Tax Exempt Number', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 35,
);
return $fields;
}
// Display Tax Exempt # Field on admin edit order page
add_filter( 'woocommerce_admin_billing_fields', 'admin_single_order_custom_addresses_fields' );
function admin_single_order_custom_addresses_fields( $fields ) {
$settings = custom_checkout_fields_settings();
foreach ( $settings as $field_key => $field_label ) {
$fields[$field_key] = array(
'label' => $field_label,
'show' => '0',
'wrapper_class' => 'form-field-wide',
);
}
return $fields;
}
// Load Ajax Tax Exempt # Field data as customer billing address field
add_filter( 'woocommerce_ajax_get_customer_details', 'add_custom_fields_to_ajax_customer_details', 10, 3 );
function add_custom_fields_to_ajax_customer_details( $data, $customer, $user_id ) {
// $settings = custom_checkout_fields_settings();
// foreach( $settings as $field_key => $field_label ) {
// $data['billing'][$field_key] = $customer->get_meta('billing_'.$field_key);
// }
// error_log(print_r($data,true));
$data['billing']['tax_exempt_number'] = $customer->get_meta('billing_tax_exempt_number');
return $data;
}
// Add Tax Exempt # Field to WooCommerce billing address in user profile
add_filter( 'woocommerce_customer_meta_fields' , 'user_profile_custom_fields' );
function user_profile_custom_fields( $fields ) {
$fields['billing']['fields']['billing_tax_exempt_number'] = array(
'label' => __( 'Tax Exempt #', 'woocommerce' ),
'description' => '',
);
return $fields;
}
// Display Tax Exempt # Field on email notifications
add_action('woocommerce_email_customer_details','display_custom_fields_on_emails_notifications', 30, 4 );
function display_custom_fields_on_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$settings = custom_checkout_fields_settings(); // Load settings
// Loop through setting fields
foreach ( $settings as $field_key => $field_label ) {
$meta_value = $order->get_meta('_billing_'.$field_key);
if ( ! empty($meta_value) ) {
echo '<div><strong>' . $field_label . ':' . '</strong> <span class="text">' . $meta_value . '</span></div>';
}
}
}
// Save the Tax Exempt # Field from the admin order edit screen and update the user meta
// Note: Allow admin to correct any errors
add_action( 'woocommerce_process_shop_order_meta', 'save_custom_billing_field_and_update_user_meta', 45, 2 );
function save_custom_billing_field_and_update_user_meta( $order_id, $post ) {
$order = wc_get_order( $order_id );
$settings = custom_checkout_fields_settings(); // Load Settings
foreach ( $settings as $field_key => $field_label ) {
if ( isset( $_POST['_billing_' . $field_key] ) ) {
// Save the order meta data
$order->update_meta_data('_billing_' . $field_key, sanitize_text_field( $_POST['_billing_' . $field_key] ));
// Also update the user meta data if a customer ID is present
if ( $order->get_customer_id() ) {
update_user_meta( $order->get_customer_id(), 'billing_' . $field_key, sanitize_text_field( $_POST['_billing_' . $field_key] ) );
}
}
}
$order->save_meta_data();
}
// Save the Tax Exempt # Field (once order is placed)
add_action( 'woocommerce_checkout_create_order', 'save_custom_billing_field_on_create_order', 20, 2 );
function save_custom_billing_field_on_create_order( $order, $data ) {
if ( isset( $_POST['billing_tax_exempt_number'] ) && ! empty( $_POST['billing_tax_exempt_number'] ) ) {
$order->update_meta_data('_billing_tax_exempt_number', sanitize_text_field( $_POST['billing_tax_exempt_number'] ) );
update_user_meta( $order->get_customer_id(), 'billing_tax_exempt_number', sanitize_text_field( $_POST['billing_tax_exempt_number'] ) );
}
}
// Display Tax Exempt # Field in order details customer section using user meta
// Note: Shows on Thank You Page if User is Logged in.
add_action( 'woocommerce_order_details_after_customer_details', 'display_tax_exempt_number_in_order_details' );
function display_tax_exempt_number_in_order_details( $order ) {
// Get the user ID from the order
$user_id = $order->get_user_id();
// Get the tax exempt number from the user meta
$tax_exempt_number = get_user_meta( $user_id, 'billing_tax_exempt_number', true );
// If it exists, display the tax exempt #
if ( ! empty( $tax_exempt_number ) ) {
echo '<p style="font-style:italic;"><strong>' . __('Tax Exempt #:', 'woocommerce') . '</strong> ' . esc_html( $tax_exempt_number ) . '</p>';
}
}
intothevoid0 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.