WooCommerce Code Snippet for Role Based Sales Tax Exemption

Role-Based Tax Exemption in WooCommerce


Role-Based Tax Exemption in WooCommerce

Below is a complete, clean, from-scratch implementation guide for setting up role-based tax exemption in WooCommerce using User Role Editor + WPCode. I wrote this for a live production site, so every step includes verification.

 

Role-Based Tax Exemption in WooCommerce

Full Implementation Guide (From Scratch)

Goal

I make WooCommerce charge $0 sales tax for customers assigned to a special role (example: Tax Exempt Customer) while charging tax normally for everyone else.

 

0) Pre-Flight Checklist (Production Safety)

Before I change anything, I confirm:

  1. I have an admin login with access to:

    • Plugins

    • Users

    • WooCommerce settings

    • WPCode

  2. I have at least one real product that normally gets taxed.

  3. I have a way to test without impacting real customers:

    • A test user account, and

    • Incognito/private browser window

 

1) Create a “Tax Exempt” Tax Class in WooCommerce

1.1 Enable additional tax classes

  1. Go to WooCommerce → Settings → Tax

  2. Scroll to Additional tax classes

  3. Add a new line:

    • Tax Exempt

  4. Click Save changes

1.2 Confirm the tax class exists

  1. Still in WooCommerce → Settings → Tax

  2. Look at the tabs across the top

  3. Confirm you now see a tab named:

    • Tax Exempt rates

If that tab exists, the tax class exists.

1.3 Confirm no rates are defined for Tax Exempt

  1. Click Tax Exempt rates

  2. Confirm the table is empty

    • No rows present

Why: If you add any rates here, you may accidentally tax “exempt” orders.

 

2) Install and Activate Required Plugins

2.1 WPCode (Code Snippets)

  1. Go to Plugins → Add New

  2. Search: WPCode

  3. Install and Activate

  4. Confirm you see Code Snippets in the left menu

2.2 User Role Editor

  1. Go to Plugins → Add New

  2. Search: User Role Editor

  3. Install and Activate

  4. Confirm you see User Role Editor under Users in the left menu

 

3) Create a New User Role: “Tax Exempt Customer”

3.1 Open User Role Editor

  1. Go to Users → User Role Editor

3.2 Create the role (recommended approach)

  1. Click Add Role

  2. Display Role Name:

    1. Tax Exempt Customer

  3. Role ID (Slug):

    1. tax_exempt_customer

  4. Make Copy Of:

    1. Select Customer

  5. Save

3.3 Confirm Tax Exempt Customer looks like Customer

Both roles should only have Read capabilities under General.

3.4 Confirm role is selectable on a user

  1. Go to Users → All Users

  2. Edit any test user

  3. Find the Role dropdown

  4. Confirm you see:

    • Tax Exempt Customer

 

4) Add the Production Snippet (WPCode)

4.1 Create the snippet

  1. Go to Code Snippets → Add Snippet

  2. Choose Add Your Custom Code (New Snippet)

4.2 Configure snippet settings

  • Snippet Title:
    Sales Tax Exempt - Users with Tax Exempt role pay no sales tax

  • Code Type:
    PHP Snippet

  • Insertion:
    Auto Insert / Run Everywhere

  • Leave Testing Mode OFF

4.3 Paste the code exactly

<?php

/**

 * Apply Tax Exempt product tax class for users with role: tax_exempt_customer

 */

 

function phd_is_tax_exempt_user() : bool {

    return is_user_logged_in()

        && in_array('tax_exempt_customer', (array) wp_get_current_user()->roles, true);

}

 

// Simple products

add_filter('woocommerce_product_get_tax_class', function ($tax_class, $product) {

    if (phd_is_tax_exempt_user()) {

        return 'tax-exempt';

    }

    return $tax_class;

}, 9999, 2);

 

// Variations

add_filter('woocommerce_product_variation_get_tax_class', function ($tax_class, $product) {

    if (phd_is_tax_exempt_user()) {

        return 'tax-exempt';

    }

    return $tax_class;

}, 9999, 2);

 

4.4 Activate the snippet

  1. Toggle the snippet Active

  2. Click Save / Update

 

5) Assign the Tax Exempt Role to a Customer

5.1 Assign the role

  1. Go to Users → All Users

  2. Edit your test customer

  3. Set Role to:

    • Tax Exempt Customer

  4. Save user

 

6) Validation Test (No Assumptions)

6.1 Test as a normal customer (baseline)

  1. Open Incognito

  2. Log in as a normal customer (not tax exempt)

  3. Add a taxable product to cart

  4. Go to cart and confirm:

    • Sales tax appears (non-zero)

6.2 Test as the tax-exempt customer

  1. Open a new incognito window (fresh session)

  2. Log in as the Tax Exempt Customer

  3. Empty cart completely

  4. Add the same taxable product

  5. Go to cart totals

Expected result

  • The Sales Tax line disappears
    or

  • Sales tax shows $0.00

If you see this, the implementation is confirmed.

 

7) Operational Use (Day-to-Day)

To make any customer tax exempt

  1. Edit user

  2. Set role to Tax Exempt Customer

  3. Save

To remove tax exemption

  1. Edit user

  2. Change role back to Customer

  3. Save

 

8) Maintenance Notes and Guardrails

Keep product settings normal

  • Do not manually change product tax class to “Tax Exempt”

  • Leave products as Standard/blank

Keep Tax Exempt rates empty

  • Do not enter Tax Exempt rates in WooCommerce

  • You want no tax rules for this class

If you ever change the role slug

  • You must update the snippet:

    • 'tax_exempt_customer'

 

9) Rollback Plan (Instant)

If anything goes wrong:

  1. Go to WPCode → Code Snippets

  2. Toggle the tax exemption snippet OFF

This immediately restores normal tax behavior.

 

If you want, I can also create a short internal SOP version for your team, plus a “quick checklist” they can follow when they mark customers tax exempt.