Add Tax Exempt form on checkout in woocommerce
Alright, I finally figured it out in case anyone is interested.
In my plugin, I made a form after the order notes by hooking in to this function: 'woocommerce_before_order_notes'
add_action('woocommerce_before_order_notes', array(&$this, 'taxexempt_before_order_notes') );
my 'taxexempt_before_order_notes' function contained:
function taxexempt_before_order_notes( $checkout ) { echo '<div style="clear: both"></div> <h3>Tax Exempt Details</h3>'; woocommerce_form_field( 'tax_exempt_checkbox', array( 'type' => 'checkbox', 'class' => array('tiri taxexempt'),array( 'form-row-wide', 'address-field' ), 'label' => __('Tax Exempt'), ), $checkout->get_value( 'tax_exempt_checkbox' )); woocommerce_form_field( 'tax_exempt_name', array( 'type' => 'text', 'class' => array('form-row-first', 'tiri', 'taxexempt', 'textbox', 'hidden'), 'label' => __('Tax Exempt Name'), ), $checkout->get_value( 'tax_exempt_name' )); woocommerce_form_field( 'tax_exempt_id', array( 'type' => 'text', 'class' => array('form-row-last', 'tiri', 'taxexempt', 'textbox', 'hidden', 'update_totals_on_change'), 'label' => __('Tax Exempt Id'), ), $checkout->get_value( 'tax_exempt_id' )); }
Then the most important woocommerce function to hook was: 'woocommerce_checkout_update_order_review'
add_action( 'woocommerce_checkout_update_order_review', array(&$this, 'taxexempt_checkout_update_order_review' ));function taxexempt_checkout_update_order_review( $post_data ) { global $woocommerce; $woocommerce->customer->set_is_vat_exempt(FALSE); parse_str($post_data); if ( isset($tax_exempt_checkbox) && isset($tax_exempt_id) && $tax_exempt_checkbox == '1' && !empty($tax_exempt_id)) $woocommerce->customer->set_is_vat_exempt(true); }
I simply parsed out the $post_data that is the serialized form data from the checkout.js file in woocommerce and checked if my part of the form was filled out correctly.
If it was, then I would set the tax exempt for the user.
The accepted solution didn't work for me, but I modified it to use the following:
//=============================================================================// ADD TAX EXEMPT CHECKMARK// =============================================================================add_action( 'woocommerce_after_order_notes', 'qd_tax_exempt');function qd_tax_exempt( $checkout ) { echo '<div id="qd-tax-exempt"><h3>'.__('Tax Exempt').'</h3>'; woocommerce_form_field( 'shipping_method_tax_exempt', array( 'type' => 'checkbox', 'class' => array(), 'label' => __('My organization is tax exempt.'), 'required' => false, ), $checkout->get_value( 'shipping_method_tax_exempt' )); echo '</div>';}add_action( 'woocommerce_checkout_update_order_review', 'taxexempt_checkout_update_order_review');function taxexempt_checkout_update_order_review( $post_data ) { global $woocommerce; $woocommerce->customer->set_is_vat_exempt(FALSE); parse_str($post_data); if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == '1') $woocommerce->customer->set_is_vat_exempt(true); }
The key here is understanding that any field with a name that starts with shipping_method
is going to inherit this updating order functionality (which was the part that didn't work for me). I found this answer at http://www.affectivia.com/blog/have-a-checkbox-on-the-checkout-page-which-updates-the-order-totals/
After a long search I found that there is a method for the cart object called remove_taxes() .So, after setting a user meta for the tax exempt users, this cancels the tax totals.
function remove_tax_for_exempt( $cart ) { global $current_user; $ok_taxexp = get_the_author_meta( 'granted_taxexempt', $current_user->ID ); if ($ok_taxexp){ // now 0 the tax if user is tax exempt $cart->remove_taxes(); } return $cart;} add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );