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:
-
I have an admin login with access to:
-
Plugins
-
Users
-
WooCommerce settings
-
WPCode
-
-
I have at least one real product that normally gets taxed.
-
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
-
Go to WooCommerce → Settings → Tax
-
Scroll to Additional tax classes
-
Add a new line:
-
Tax Exempt
-
-
Click Save changes
1.2 Confirm the tax class exists
-
Still in WooCommerce → Settings → Tax
-
Look at the tabs across the top
-
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
-
Click Tax Exempt rates
-
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)
-
Go to Plugins → Add New
-
Search: WPCode
-
Install and Activate
-
Confirm you see Code Snippets in the left menu
2.2 User Role Editor
-
Go to Plugins → Add New
-
Search: User Role Editor
-
Install and Activate
-
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
-
Go to Users → User Role Editor
3.2 Create the role (recommended approach)
-
Click Add Role
-
Display Role Name:
-
Tax Exempt Customer
-
-
Role ID (Slug):
-
tax_exempt_customer
-
-
Make Copy Of:
-
Select Customer
-
-
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
-
Go to Users → All Users
-
Edit any test user
-
Find the Role dropdown
-
Confirm you see:
-
Tax Exempt Customer
-
4) Add the Production Snippet (WPCode)
4.1 Create the snippet
-
Go to Code Snippets → Add Snippet
-
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
-
Toggle the snippet Active
-
Click Save / Update
5) Assign the Tax Exempt Role to a Customer
5.1 Assign the role
-
Go to Users → All Users
-
Edit your test customer
-
Set Role to:
-
Tax Exempt Customer
-
-
Save user
6) Validation Test (No Assumptions)
6.1 Test as a normal customer (baseline)
-
Open Incognito
-
Log in as a normal customer (not tax exempt)
-
Add a taxable product to cart
-
Go to cart and confirm:
-
Sales tax appears (non-zero)
-
6.2 Test as the tax-exempt customer
-
Open a new incognito window (fresh session)
-
Log in as the Tax Exempt Customer
-
Empty cart completely
-
Add the same taxable product
-
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
-
Edit user
-
Set role to Tax Exempt Customer
-
Save
To remove tax exemption
-
Edit user
-
Change role back to Customer
-
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:
-
Go to WPCode → Code Snippets
-
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.