I’ve been working on a WooCommerce project recently, the client asked if I could isolate certain users who would need to be exempt from being charged any tax or VAT on products sold through their WooCommerce shop. This particular shop was behind the standard WordPress login so it was simply a case of:

  • adding a checkbox to the user’s profile to identify if they were exempt from paying tax or VAT, this would only visible to an Administrator.
  • use a WooCommerce hook to enable or disable tax charges at the point of checkout for the currently logged in user.

The user has to log in to purchase anything from the shop but they could never get to the /wp-admin/profile.php because WooCommerce, by default, creates all the user’s profile information in the frontend. So, it was just a case of getting the user ID after they log in, check if they have been set as an exempt tax or VAT payer, then filter to enable/disable the tax setting from WooCommerce on the checkout page because this is where the tax is added to the purchase.

WooCommerce has a filter hook for this, it filters the tax setting. Depending on the way the shop’s tax setting has been set up the filter can technically be switched to true or false when it’s loaded on the checkout page.

Because I’m sharing this as a plugin, the first thing to do is check if WooCommerce is even active on the site, we can do that on the activation of this plugin using the activation hook:

function is_woocommerce_active() {
    if (is_plugin_inactive( 'woocommerce/woocommerce.php' )) {
        wp_die('WooCommerce needs to be activated before running this plugin!');
    }     
}
register_activation_hook( __FILE__, 'is_woocommerce_active' );

Then we need the ability to edit/show the Administrator the setting for any particular user on the user’s profile page:

function user_profile_form_callback(WP_User $user) {
    require_once plugin_dir_path( dirname( __FILE__ ) ) . 'wooc-user-tax-exempt/wooc-user-tax-exempt-form.php';
}
add_action('show_user_profile', 'user_profile_form_callback'); // editing your own profile
add_action('edit_user_profile', 'user_profile_form_callback'); // editing another user

We also need to allow the Administrator to save the users metadata to their profile by the user’s ID:

function user_profile_update_callback($user_id) {
    if (!current_user_can('edit_user', $user_id)) {
        return;
    }
    
    update_user_meta($user_id, 'user_is_vat_exempt', $_POST['user_is_vat_exempt']);
}
add_action('personal_options_update', 'user_profile_update_callback');
add_action('edit_user_profile_update', 'user_profile_update_callback');

Now all you need to do is filter the WooCommerce tax setting. For most users, the tax is charged at the checkout, only a small number of users would be exempt so we only need to check that the user is exempt, if so then switch the setting from true to false:

function wooc_user_tax_exempt_callback($wc_tax_enabled) {

    $user = wp_get_current_user();
    $vat_exempt = get_user_meta($user->ID, 'user_is_vat_exempt', true); 
    if ($vat_exempt != null) {
        $wc_tax_enabled = false;
    }
    return $wc_tax_enabled;

}
add_filter( 'wc_tax_enabled', 'wooc_user_tax_exempt_callback', 10, 1);

And that it! That’s how to make a user exempt from tax charges if you are using WooCommerce.

The bundled plugin can be found on github https://github.com/eirichmond/wooc-user-tax-exempt

Leave a comment

Your email address will not be published. Required fields are marked *