How to show the data of the new fields in the WooCommerce billing data?
I have created on the page "My Account" of WooCommerce, in the billing address, two new fields.
These are only visible to certain user roles (‘administrator’),
The fields are shown at checkout in this screenshot
1 – a checkbox that has a text label: 'label' => __('I have Surcharge'
(which is the one I need to show in the emails)
This field is created with the following code and
add_filter( 'woocommerce_billing_fields', 'custom_checkbox_below_billing_company' );
function custom_checkbox_below_billing_company( $fields ) {
//Show the checkbox only to users with the role "administratorr"
if ( user_can( wp_get_current_user(), 'administrator' ) ) {
$fields['billing_custom_checkbox'] = array(
'type' => 'checkbox',
'label' => __('Tengo Recargo', 'woocommerce'),
'class' => array('form-row-wide'),
'priority' => 30,
'required' => false,
'clear' => true,
'default' => 0,
);
}
return $fields;
}
// Save the value of the checkbox in the database
add_action( 'woocommerce_customer_save_address', 'save_billing_custom_checkbox', 10, 2 );
function save_billing_custom_checkbox( $customer_id, $load_address ) {
if ( isset( $_POST['billing_custom_checkbox'] ) ) {
$checkbox_value = sanitize_text_field( $_POST['billing_custom_checkbox'] );
update_user_meta( $customer_id, 'billing_custom_checkbox', $checkbox_value );
} else {
update_user_meta( $customer_id, 'billing_custom_checkbox', '0' );
}
}
/* SENDING THE FIELD IN THE POST*/
// Show checkbox label in billing address
add_action( 'woocommerce_admin_billing_fields', 'show_billing_custom_checkbox_label' );
add_action( 'woocommerce_billing_fields', 'show_billing_custom_checkbox_label' );
function show_billing_custom_checkbox_label( $fields ) {
if ( isset( $fields['billing_custom_checkbox'] ) ) {
$fields['billing_custom_checkbox']['label'] .= ' ' . __('(Tengo Recargo de Ekivalencia)', 'woocommerce');
}
return $fields;
}
add_filter( 'woocommerce_email_customer_details_fields', 'add_custom_checkbox_to_emails', 10, 3 );
function add_custom_checkbox_to_emails( $fields, $sent_to_admin, $order ) {
$checkbox_value = get_user_meta( $order->get_user_id(), 'billing_custom_checkbox', true ); // Get the value of the checkbox
$fields['billing_custom_checkbox'] = array(
'label' => __('Tengo Recargo de Ekivalencia', 'woocommerce') . ' ' . ($checkbox_value ? 'SI' : 'NO'), //Add the value of the checkbox in the label
'value' => $checkbox_value ? 'SI' : 'NO', // Agrega el valor del checkbox en el valor
);
return $fields;
}
2 – A field to enter the tax identification number of the company, NIF/CIF : 'label' => __('NIF/CIF', 'woocommerce'),
The NIF/CIF field has been created as follows, but the way it is displayed in emails to clients and administrators fails
add_filter('woocommerce_checkout_fields', 'add_custom_billing_field');
function add_custom_billing_field($fields)
{
// Get the role of the current user
$user = get_userdata( get_current_user_id() );
if ( ! empty( $user ) && in_array( 'administrator', (array) $user->roles ) ) {
$current_user = wp_get_current_user();
$saved_license_no = $current_user->license_no;
// Add the NIF/CIF field to the billing address editing form
$fields['billing_license_no'] = array(
'label' => __('NIF/CIF', 'woocommerce'),
'placeholder' => __('B12345678', 'woocommerce'),
'required' => true,
'clear' => false,
'type' => 'text',
'default' => $saved_license_no,
'class' => array('form-row-wide'),
'priority' => 25,
);
}
//Return the updated array of billing fields
return $fields;
}
/* save the new field and show it in email, checkout, etc... */
add_action('woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field');
function bbloomer_save_new_checkout_field($order_id)
{
if (isset($_POST['billing_license_no'])) { //Check if field value was sent
update_post_meta($order_id, '_license_no', wc_clean($_POST['billing_license_no'])); //Clear value before saving
}
}
add_action('woocommerce_thankyou', 'bbloomer_show_new_checkout_field_thankyou');
function bbloomer_show_new_checkout_field_thankyou($order_id)
{
if (get_post_meta($order_id, '_license_no', true)) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta($order_id, '_license_no', true) . '</p>';
}
add_filter('woocommerce_order_details_after_order_table', 'bbloomer_show_new_checkout_field_order', 20, 1);
add_action('woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order');
function bbloomer_show_new_checkout_field_order($order)
{
$order_id = $order->get_id();
if (get_post_meta($order_id, '_license_no', true)) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta($order_id, '_license_no', true) . '</p>';
}
add_action('woocommerce_email_after_order_table', 'bbloomer_show_new_checkout_field_emails', 20, 4);
function bbloomer_show_new_checkout_field_emails($order, $sent_to_admin, $plain_text, $email)
{
if (get_post_meta($order->get_id(), '_license_no', true)) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta($order->get_id(), '_license_no', true) . '</p>';
}
These fields are edited from the user’s account page, my-account/edit-address/billing/ and are also displayed at Checkout when placing an order.
I only need these data to be part of the WooCommerce billing data and to be sent along with the rest of the data: Name, Surname, Company Name, Address, etc.
It can be seen on the screenshot. //// admin-Mail
Now they are sent in the mail but outside the address range, as you can see in the screenshot (administrator-Mail.png)
But this data is not part of the WooCommerce structure, so it is not displayed when I generate an invoice (invoice screenshot – bill.jpg)
What should I do so that these data are part of the WooCommerce Billing address?
Perhaps it could be achieved by editing the WooCommerce structure, but I don’t know if this is possible, and if it could be done, how would it be done?
How can this be done in the safest way?
2
Answers
To show the data of new fields in the WooCommerce billing data, you’ll need to add custom code to:
Here’s an example code snippet that demonstrates how to add a new field for Company Name to the WooCommerce billing form and save the data to the order meta:
In this example code, we’re using the
woocommerce_billing_fields
filter to add a new field for "Company Name" to the billing form. We’re then using thewoocommerce_checkout_create_order
action to save the data from this field to the order meta data.Finally, we’re using the
woocommerce_order_formatted_billing_address
filter to display the Company Name in the billing address section of the order details page and emails.You can customize the field name, label, and meta key as per your requirements. Additionally, you may need to update other functions that use the billing data to include the new field data as well.
Test it and let me know for any change or if error occure