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 wich 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
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+
Related
I set 2 methods of delivery:
Express shipping 48 hours at a cost which es a % of the total cart $ amount
Normal Free shipping in 29 days
The functionality that I want is the following:
If the stock of the product is = 0 or less than 0 (backorder is allowed), the Express shipping option should be hidden. Just must show the Normal free shipping option.
If the stock of the product is >0, both options should be shown.
If I selected more than one product, and one or more of the products does not have stock, the Express shipping option also should be hidden.
From another answer related to this question, I tried the script showed in the answer.
The problem is the following: in the last part of the script there is a mention to a variable in an external plugin.
How I adapt the code to a more generic woocommerce without that plugin?
Script:
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_quantity' , 10, 1 );
function check_cart_for_oos() {
// load the contents of the cart into an array.
global $woocommerce;
$found = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$_product_quantity = $_product->get_stock_quantity();
$_cart_quantity = $values['quantity'];
if (($_product_quantity <= 0) || ($_cart_quantity > $_product_quantity)) {
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_quantity( $available_methods ) {
// use the function check_cart_for_oos() to check the cart for products with 0 stock.
if ( check_cart_for_oos() ) {
// remove the rate you want
unset( $available_methods['table_rate_shipping_next-day'] ); // Replace "table_rate_shipping_next-day" with "table_rate_shipping_your-identifier".
}
// return the available methods without the one you unset.
return $available_methods;
}
From: Remove next day shipping method for out of stock items in Woocommerce
In that answer, it's already mentioned to use your own shipping method identifier. So you just need to replace the shipping method identifier with yours.
// use the function check_cart_for_oos() to check the cart for products with 0 stock.
if ( check_cart_for_oos() ) {
// remove the rate you want
unset( $available_methods['USE_YOUR_OWN_IDENTIFIER_HERE'] ); // Replace "table_rate_shipping_next-day" with "table_rate_shipping_your-identifier".
}
I would like to exclude all tax rates from a specific country inside my checkout containing a specific product (which is a custom fee (tip)) - I tried to use another Tax rates like 'Zero Tax Rates' in woocommerce to set Canada to 0%, But I have an issue with all the other countries using this method as only Standard Rate seems to calculate the rates based on the total (including the tip).
I found this code to exclude a all tax rates based on a zip code :
/**
* #snippet Remove Tax if Field Value Exists - WooCommerce Checkout
* #how-to Get CustomizeWoo.com FREE
* #author Rodolfo Melogli, BusinessBloomer.com
* #testedwith WooCommerce 4.5
* #donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );
function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
WC()->customer->set_is_vat_exempt( false );
parse_str( $post_data, $output );
if ( $output['billing_postcode'] === '32444' ) WC()->customer->set_is_vat_exempt( true );
}
I'm able to edit it to choose 'Billing_country' === CA' instead but I would also need this function to target a specific product so that the rest of the website would not use this function. I need Canadians customers to get no taxes rates for this specific products, but not for the others countries.
Thanks for your help.
I am on the verge of losing it with this problem. So what I am attempting is to apply Zero rate tax rate when a specific coupon ('12345') is applied to the order. This site also has the plugin WooCommerce Distance Rate Shipping installed which adds its own costs and tax to the order based on billing distance.
While the billing address fields are empty, the following code successfully detects the coupon in question and sets tax rate to 'Zero Rate':
function apply_matched_coupons() {
$coupon_code = '12345';
if ( !is_admin() && WC()->cart->has_discount( $coupon_code ) ) {
$tax_class = 'Zero rate';
}
return $tax_class;
};
add_action( 'woocommerce_product_tax_class','apply_matched_coupons' );
But when an address is typed in the billing fields, the Woocommerce order update AJAX works and the WooCommerce Distance Rate Shipping plugin reads the billing addresss, re-checks its rules, applies its costs and also adds its tax. Hence, overwriting my Zero Tax rate set earlier. Screenshot is attached. I believe a hook like woocommerce_checkout_update_order_review may work which fires each time the order is updated on checkout, but can't figure out exactly how to set it.
The plugin's page here does show an example to set the $rule_cost to 0 with this code:
add_filter( 'woocommerce_distance_rate_shipping_rule_cost_distance_shipping', function( $rule_cost, $rule, $distance, $package ) {
$order_total = $package['contents_cost'];
if ( $order_total > 100 && $distance <= 5 ) {
$rule_cost = 0;
}
return $rule_cost;
}, 10, 4 );
Could this help in any way?
screenshot of extra tax added by the said plugin
I have a website where customer chooses a zone in a dropdown in billing fields. These are 4 zones: zone1, zone2 and zone3 as an example.
The idea is that if customer chose zone1 and total cart is less than $1000, customer needs to pay a shipping rate of $100. If more than $1000, customer gets free shipping.
I need to replicate that for the other 2 zones with different cart amounts and shipping rates. Is this possible?
Right now, I have the following working code but customer has to manually choose a flat rate. Problem is, of course, customers... If free shipping is displayed, they will choose that one knowing they dont apply for that option when total amount is less for their zone.
/* Change shipping based on total cost purchased. */
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 );
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
$threshold_centro = 1000;
$threshold_hasta_el_5 = 1500;
$threshold_arelauquen = 2000;
if ( WC()->cart-> cart_contents_total < $threshold_centro ) {
unset( $rates['free_shipping:9'] );
}
elseif ( WC()->cart-> cart_contents_total < $threshold_hasta_el_5 ) {
unset( $rates['flat_rate:2'] );
}
elseif ( WC()->cart-> cart_contents_total < $threshold_arelauquen ) {
unset( $rates['flat_rate:2'], $rates['flat_rate:3'] );
}
else {
unset( $rates['flat_rate:2'], $rates['flat_rate:3'], $rates['flat_rate:4'], $rates['flat_rate:5'], $rates['flat_rate:6'] );
}
return $rates;
}
and the custom field is this one: billing_wcccf_id_qXZZegrC774HULq
Try to set a WC session when you trigger the selectbox and the use it in your if statement to get the right shipping rate or use a switch
WC->session->set('your session name', 'value')
WC->session->get('your session')
SCENARIO :
We are an online multi-restaurant delivery service :
And we need to add an extra fee if the customer orders from 2 or more different restaurants. So for each restaurant we add an extra 2€ fee.
EXAMPLES :
customer selects 1 meal : no fee
customer selects 2 meals from same restaurant : no fee
customer selects 2 meals from 2 diff restaurants : 2€ extra fee
customer selects 3 meals from 3 diff restaurants : 2€ + 2€ = 4€
customer selects 4 meals from 4 diff restaurants : 2€ + 2€ +2€ = 6€
... and so on
CATEGORIES :
Each restaurant is 1 category
Each meal has more than 1 category AND
We need to check if this category in particular is equal or different for each meal on cart checkout
PROBLEM :
Here's what we think might work :
We might have to scroll thru each meal (product) on cart checkout AND
We have to get the categories for each meal AND
Compare each meal + check if they belong to diff restaurants
Restaurants are a sub-category like :
City (top cat.) -> Restaurant (child cat.) -> Meal (product)
SOLUTION :
So we think might be easy to write some code that checks if products (meals) on cart checkout are from more than 1 category (restaurant) ? And then apply the additional fee ?
Need some help here on how to best approach this scenario. Thank you!
To do so, add follows code snippet in your active theme's functions.php -
function woo_add_cart_fee() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$restaurant = array();
foreach($items as $item => $values) {
$available_restaurant_cat_ids = array(); // Replace the empty array with all avaiable 'restaurants' categories ids
$restaurant_list_ids = wp_get_post_terms($values['data']->get_id(),'product_cat',array('fields'=>'ids')); // we assumed that restaurant belongs to product_cat
if($restaurant_list_ids){
foreach ($restaurant_list_ids as $id) {
if(!in_array($id, $available_restaurant_cat_ids)) continue; // id not belongs to restaurant
if(!in_array($id, $restaurant)){
$restaurant[] = $id;
}
}
}
}
$restaurant_multiply = count($restaurant) - 1; // first restaurant id free
$extra_fee = 2; // 2 euro
$woocommerce->cart->add_fee( __('Extra fees', 'woocommerce'), $extra_fee*$restaurant_multiply );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );