Apply tax exempt or special tax rates based on user role

General Discussion 2 posts 2 voices

  1. Hi,

    I use WooCommerce and Members plugin. I have created special tax rate classes (CustomerTaxExemptTaxClass & CustomerPSTExemptTaxClass for customers who can be tax exempt or pay a certain tax only (pay only PST tax in Canada). I have created two new roles with the members plugin: customer_tax_exempt (Customer Tax Exempt) & customer_pst_exempt (Customer PST Exempt).

    I have added the following code into my Child Theme functions.php file, however it does not work.

    /* APPLY DIFFERENT TAX RATE BASED ON CUSTOMER USER ROLE */
    /* special tax rate: zero if role: Customer Tax Exempt */
    function CustomerTaxExemptTaxClass( $tax_class, $product ) {
      global $current_user;
    
        if (is_user_logged_in() && current_user_can('customer_tax_exempt ')) {
            $tax_class = 'CustomerTaxExemptClass';
        }               
    
      return $tax_class;
    }
    
    add_filter( 'woocommerce_product_tax_class', 'CustomerTaxExemptTaxClass', 1, 2 );
    
    /* special tax rate: charge only GST if role: Customer PST Exempt */
    function CustomerPSTExemptTaxClass( $tax_class, $product ) {
      global $current_user;
    
        if (is_user_logged_in() && current_user_can('customer_pst_exempt')) {
            $tax_class = 'CustomerPSTExemptClass';
        }               
    
      return $tax_class;
    }
    
    add_filter( 'woocommerce_product_tax_class', 'CustomerPSTExemptTaxClass', 1, 2 );

    Any help in figuring this out would be greatly appreciated.

    Thank you
    Lyse

  2. First, you really shouldn’t write code based on roles. Write code based on capabilities. So, if you have a role (or multiple roles) to avoid taxes, create an avoid_taxes capability, for example, and assign it to the necessary roles.

    Second, this code should go into a plugin, not your theme. Here’s a tutorial on that if you don’t know how: http://justintadlock.com/archives/2011/02/02/creating-a-custom-functions-plugin-for-end-users

    Now, on to the actual code. I’m not familiar with WC at all. I don’t use the plugin because it’s a mess of cobbled-together code that even confuses me. 🙂 However, assuming your code is generally correct and you’re using the correct filter hook, here’s what that code should look like (using the avoid_taxes capability mentioned above):

    add_filter( 'woocommerce_product_tax_class', 'th_filter_product_tax_class' );
    
    function th_filter_product_tax_class( $class ) {
    
    	return current_user_can( 'avoid_taxes' ) ? 'CustomerTaxExemptClass' : $class;
    }