Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Tax Exempt form on checkout in woocommerce

I am trying to add a form to my checkout page so when a user clicks the 'Tax Exempt' checkbox, a textbox will popup and ask the user what the Tax Exempt Id number is.

I got all of that working great, and I even added the update_totals_on_change class to my form field so it will update the totals.

My next step was to add an action/filter on a method so when the update_totals_on_change executes, I can set the tax to 0 and then it will finish calculating the total.

Does anyone know which functions I can hook on to?

Looking at the checkout.js file in WooCommerce, they set the action to woocommerce_update_order_review for the ajax operation.

I tried following that but soon got lost.

I was thinking I could add some post data by hooking in to woocommerce_checkout_update_order_review

and then hooking in to woocommerce_before_calculate_totals to modify the tax stuff, but I have no idea what I need to modify.

Am I even on the right path?

like image 476
Craig Avatar asked May 31 '13 23:05

Craig


1 Answers

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.

like image 72
Craig Avatar answered Nov 15 '22 11:11

Craig