Skip to content

Instantly share code, notes, and snippets.

@colewinans
Created December 11, 2013 17:24
Show Gist options
  • Save colewinans/7914695 to your computer and use it in GitHub Desktop.
Save colewinans/7914695 to your computer and use it in GitHub Desktop.
WooCommerce – Tax Exempt
/**
* Add tax exempt fields to checkout
**/
add_action('woocommerce_before_order_notes', 'taxexempt_before_order_notes');
function taxexempt_before_order_notes( $checkout ) {
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' ));
}
/**
* Remove taxes from order if user is tax exempt
**/
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($tax_exempt_checkbox) && isset($tax_exempt_id) && $tax_exempt_checkbox == '1' && !empty($tax_exempt_id))
$woocommerce->customer->set_is_vat_exempt(true);
}
/**
* Update the order meta with 'Tax Exempt Name' and 'Tax Exempt ID'
**/
add_action('woocommerce_checkout_update_order_meta', 'taxexempt_checkout_update_order_meta');
function taxexempt_checkout_update_order_meta( $order_id ) {
if ($_POST['tax_exempt_name']) update_post_meta( $order_id, 'tax_exempt_name', esc_attr($_POST['tax_exempt_name']));
if ($_POST['tax_exempt_id']) update_post_meta( $order_id, 'tax_exempt_id', esc_attr($_POST['tax_exempt_id']));
}
/**
* Display 'Tax Exempt Name' and 'Tax Exempt ID' on the order page
**/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'taxexempt_display_admin_order_meta', 10, 1 );
function taxexempt_display_admin_order_meta($order){
echo '<p><strong>'.__('Tax Exempt Name').':</strong> ' . $order->order_custom_fields['tax_exempt_name'][0] . '</p>';
echo '<p><strong>'.__('Tax Exempt ID').':</strong> ' . $order->order_custom_fields['tax_exempt_id'][0] . '</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment