How can I correctly set up a custom WooCommerce checkout field that can be edited by both the user and admin?

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:

  1. I used the woocommerce_billing_fields filter to add a new field for the Tax Exempt Number to the billing section of the checkout.
  2. I utilized the woocommerce_checkout_create_order action to save the field data to both the order meta and the user meta.
  3. 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.
  1. Allowed admin to correct field value using the woocommerce_process_shop_order_meta action.
  2. 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). And woocommerce_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>';
    }
}

New contributor

intothevoid0 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật