2

In My WordPress e-commerce web site I use WP Hotel Booking, a plugin for hotel room bookings. The checkout process is done using WooCommerce.

The Issue: We have different rooms with different pricing. For example:

  • Room A price - 1500
  • Room B Price - 2700
  • Room c price - 2200

GST Tax is set at 12% for rooms which price is below 2500 and 18% for rooms above 2500.

Since I am using WP Hotel Booking for this custom product (room Management), I am unable to use the Additional Tax Classes option in WooCommerce to set different tax classes.

I need your help in writing a function to check the room value and then decide what tax needs to be set for the given room.

Thanks

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Arun
  • 43
  • 1
  • 5

1 Answers1

3

This is something accessible and easy.

1°) you need to create in your WooCommerce Tax settings 2 new Tax classes. In this example I have named that tax classes "Tax 12" and "Tax 18". Then for each of them you will have to set a different percentage of 12% and 18%.

2°) Now here is a custom function hooked in woocommerce_before_calculate_totals action hook that is going to apply a tax class based on the product price. I don't use the tax class names, but the tax class slugs, that are in lowercase and spaces are replace by a hyphen.

So Here is that code:

add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );
function change_cart_items_prices( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        // get product price
        $price = $cart_item['data']->get_price();

        // Set conditionaly based on price the tax class
        if ( $price < 2500 )
            $cart_item['data']->set_tax_class( 'tax-12' ); // below 2500
        if ( $price >= 2500 )
            $cart_item['data']->set_tax_class( 'tax-18' ); // Above 2500
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works on WooCommerce version 3+


LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, i tried your code pasted it on my functions.php on a newly installed wordpress/woocommerce. i replaced tax-12 and tax-18 with standard & zero-rate. both class are setup correct. 1. standard rate = 8% 2. zero-rate - 0% But doesnt seem to work. the tax wont show. what am i missing? thanks – Jeremi Liwanag Jun 12 '19 at 01:59
  • Thanks for this answer, however, it gets tricky if we enable the setting where price is inclusive of tax. How would we handle that? – Raj Pawan Gumdal Dec 14 '20 at 03:48
  • 1
    @RajPawanGumdal You should not set prices inclusive taxes for multiple tax rates, but always set prices exclusive taxes… – LoicTheAztec Dec 14 '20 at 07:34
  • I agree hyphothetically, however, this can get complicated and achievable. Now, let's say the tax exclusive price is for example 2510, it would mean that charging customer higher at 2510 and charging a tax of 18% will fetch the seller lesser value than charging customer lower at 2490 + a tax of 12%. In the latter case it's a win-win for both seller and the buyer too. I think we can have these logic built in for inclusive tax price to offer a value solution. – Raj Pawan Gumdal Dec 18 '20 at 06:07
  • @LoicTheAztec Please help me with the function, which is posted below I want to calculate the tax based on the entire cart subtotal?? – musthafa Jun 02 '22 at 12:06