I have tax enabled for my WooCommerce installation. It calculates tax for all customer’s who live in my state (based on their shipping address). This is the default setup for WooCommerce.
Some customers are tax exempt and shouldn’t be charged tax. I created a custom field in the user profile where I can check a box to exempt customers from being charged tax. This works correctly.
I tried to find a hook where I can use that selection to disable the tax but the code I have causes the checkout page to be blank for all users. No error message is displayed.
My functions.php code is as follows:
/////////////////////////////////////// /* Tax exempt customers */ /////////////////////////////////////// // Add tax exempt custom user field in admin add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 ); add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 ); function add_customer_tax_exempt_checkbox( $user ) { ?> <h3><?php _e("Tax status"); ?></h3> <table class="form-table"> <tr> <th><?php _e("Tax exempt"); ?></th> <td> <?php woocommerce_form_field( 'tax_exempt', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('Allowed'), ), get_user_meta( $user->id, 'tax_exempt', true ) ); ?> </td> </tr> </table> <?php } // Save allowed custom user field in admin add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' ); add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' ); function save_customer_tax_exempt_checkbox( $user_id ) { if ( current_user_can( 'edit_user', $user_id ) ) { update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' ); } } // Enabling or disabling tax calculation at checkout add_filter( 'woocommerce_product_tax_class', 'disable_tax_calculation' ); function disable_tax_calculation( $tax_class, $product ) { if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) { $tax_class = 'Zero Rate'; } return $tax_class; } /////////////////////////////////////// /* END: Tax exempt customers */ ///////////////////////////////////////
Advertisement
Answer
Since Woocommerce 3 woocommerce_product_tax_class
hook is deprecated and has been replaced. I have updated your 3rd function below:
// Add tax exempt custom user field in admin add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 ); add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 ); function add_customer_tax_exempt_checkbox( $user ) { ?> <h3><?php _e("Tax status"); ?></h3> <table class="form-table"> <tr> <th><?php _e("Tax exempt"); ?></th> <td> <?php woocommerce_form_field( 'tax_exempt', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('Allowed'), ), get_user_meta( $user->id, 'tax_exempt', true ) ); ?> </td> </tr> </table> <?php } // Save allowed custom user field in admin add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' ); add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' ); function save_customer_tax_exempt_checkbox( $user_id ) { if ( current_user_can( 'edit_user', $user_id ) ) { update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' ); } } // Enabling or disabling tax calculation at checkout add_filter( 'woocommerce_product_get_tax_class', 'disable_tax_calculation', 10, 2 ); add_filter( 'woocommerce_product_variation_get_tax_class', 'disable_tax_calculation', 10, 2 ); function disable_tax_calculation( $tax_class, $product ) { if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) { $tax_class = 'Zero Rate'; } return $tax_class; }
Code goes in functions.php file of your active child theme (or active theme). It should better work.
Related: Disable tax programmatically for a specific user role