How To Call WooCommerce Tax Classes Programatically?
-
I am using a Theme for a Hotel Booking Website which internally uses WooCommerce Checkout for Payment Processing.
We have to add hotels & rooms separately in the options given by them (Not directly as Woo Product) & then the theme dynamically creates the Hotel as a Temporary WooCommerce Product when a visitor clicks on BOOK NOW and even the TAX is also managed by WooCommerce itself.
The issue I face is: In India, There are multiple TAX (GST) rate slabs for Hotel Room Rent depending on Per Night Price.
-
If the per night price is < = INR 1000: The GST percentage is 0%
If it is from INR 1001 to INR 7500: The GST is 12%
If it is more than INR 7500: The GST is 18%I referred to this https://kraftpixel.com/gst-india-setup-woocommerce-wordpress/ where they have explained that we can do this by creating different tax classes as per our requirement and edit the products manually and update its respective tax class.
But If we have more than one room inside a hotel, The theme creates more than one temporary woo-commerce product, and updating their class for the same has become a very troublesome process.
So I was thinking, What if programmatically check the PER NIGHT PRICE (which in our case is the WooCommerce Product Price) and depending on that Call its respective Tax Class in the Cart / Checkout Page?
I also referred: https://stackoverflow.com/questions/47379965/add-custom-tax-value-at-woocommerce-checkout where they are programmatically calculating the Tax Rate like this:
// Auto Add Tax Depending On Room Per Night Price add_action( 'woocommerce_cart_calculate_fees','auto_add_tax_for_room', 10, 1 ); function auto_add_tax_for_room( $cart ) { if ( is_admin() && ! defined('DOING_AJAX') ) return; $percent = 18; // Calculation $surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100; // Add the fee (tax third argument disabled: false) $cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false ); }
How do I extend this code and call the respective WooCommerce Class here? Something like this?
$price = get_post_meta( get_the_ID(), '_regular_price', true); if { ( ' $price' <= 1000 ) tax class = ZERO RATES; elseif {( ' $price' > 1001 && <7500 ) tax class= GST 12% RATES; } else {( ' $price' > 7501) tax class = GST 18% RATES; }
Please guide me!
- The topic ‘How To Call WooCommerce Tax Classes Programatically?’ is closed to new replies.