When a user is on my woocommerce store checkout, I want to set the tax to 0 if they choose a certain payment gateway like "paypal_pro", "cheque", "bacs", or in my case "wdc_woo_credits". Which is a woocredits plugin that allows users to pay with credits instead of a credit card.
I know this function sets the taxes correctly because when I print_r($cart_object) I see I set all the taxes to 0, but yet the checkout still applies the tax in the total.
I think I need a function that recalculates the taxes after I set it using the function below.
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//if( $chosen_payment_method == 'cheque' ){
if( $chosen_payment_method == 'wdc_woo_credits' ){
$cart_object->set_cart_contents_taxes(0);
$cart_object->set_cart_contents_tax(0);
$cart_object->set_subtotal_tax(0);
$cart_object->set_shipping_tax(0);
$cart_object->set_total_tax(0);
$cart_object->set_fee_tax(0);
$cart_object->set_fee_taxes(0);
$cart_object->set_subtotal_tax(0);
foreach($cart_object->cart_contents as $product){
$cart_object->cart_contents[$product['key']]['line_tax'] = 0;
$cart_object->cart_contents[$product['key']]['line_subtotal_tax'] = 0;
$cart_object->cart_contents[$product['key']]['line_tax_data']['subtotal'] = 0;
$cart_object->cart_contents[$product['key']]['line_tax_data']['total'] = 0;
}
}
//echo '<pre>'; print_r($cart_object); echo '</pre>';
}
This function detects what payment selection they choose and re-runs the update checkout function. But yet the tax is still in the total.
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
Your store has enabled taxes (you can check this in WooCommerce > Settings > General, under the Enable taxes checkbox) Your store is enabled to automatically calculate taxes at checkout (you can check this in WooCommerce > Settings > Tax under the Automated taxes setting)
Yes, WooCommerce takes care of sales tax. You can either configure it to calculate taxes for you, or you can add your own tax rates. If you need to charge sales tax on your orders, WooCommerce makes it easy to do so.
WooCommerce Tax Service is an official Automattic extension that performs automatic tax calculations. It is powered by Jetpack and is currently available in over 30 countries, such as: the United States, Canada, Australia, Kingdom of the United, Germany, France, Spain, etc.
WooCommerce makes it easy to collect and remit sales tax by automatically calculating the correct tax rate for each customer based on their shipping address. You can also create custom tax rules for specific products or countries. To get started, you'll need to configure your tax settings in WooCommerce.
After lots of googling, i found out that I don't need to mess with the cart totals at all. I need to alter the current logged in customer.
I can do it with this function which removes tax for the current logged in user.
WC()->customer->set_is_vat_exempt( true );
all this goes in functions.php. You still need the woocommerce_review_order_before_payment function from above which triggers the ajax. So here's the complete code to hide tax on a certain payment gateway for only logged in users.
// calculate fees on checkout page
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//if( $chosen_payment_method == 'cheque' ){
if( $chosen_payment_method == 'wdc_woo_credits' ){
// if user buys with credits, dont allow tax.
WC()->customer->set_is_vat_exempt( true );
}else{
// if user buys with credit card, allow tax
WC()->customer->set_is_vat_exempt( false );
}
}
// refresh the checkout page totals once user selects
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With