1
\$\begingroup\$

I have implemented a custom WooCommerce checkout field to collect a Tax Exempt Number in the billing section. This field's value is stored in the user meta upon checkout to facilitate future orders.

The functionality includes:

  • Displaying the field on the thank you page and the "View Order" page (/my-account/view-order/#/) for logged-in customers.

  • Including the field value in emails sent to guests, customers, and admins.

  • Allowing customers to edit the field value in the Addresses section of their account (/my-account/edit-address/billing/).

  • Allowing admins to edit the field value on the order edit page and the WordPress user edit page.

  • Loading the field value via AJAX when a customer is selected for a manual order.

<?php

/**
 * 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 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 ) {
    $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
    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
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
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>';
    }
}
\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.