Show only one Shipping rate based on cart amount - php

Using Woocommerce in WordPress
I am trying to find a way to use 2 shipping charges:
If a customer order is less than n amount then shipping is standard x amount
If a customer order is more than n amount then shipping is reduced to x amount
I also need to hide the standard if the order qualifies for reduced shipping.
Currently woocommerce displays both shipping charges if the order qualifies for reduced shipping
Any ideas how i can resolve this please?
Thanks

Here is a custom function hooked in woocommerce_package_rates action hook that will show only one shipping rate based on the cart amount limit:
add_filter( 'woocommerce_package_rates', 'hide_sshipping_rate_based_on_cart_amount', 100 );
function hide_sshipping_rate_based_on_cart_amount( $rates ) {
// HERE define the limit amount to switch of shipping rate (integer)
$amout_limit = 600;
// Cart total amount (integer)
$cart_total = WC()->cart->cart_contents_total;
// Set below your 2 Rate ID slugs
if( $cart_total >= $amout_limit )
unset( $rates['flat_rate:12'] ); // Removes the LOWER Rate
else
unset( $rates['flat_rate:8'] ); // Removes the HIGHER Rate
return $rates;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Related

Apply coupon after calculate the shipping rate - woocommerce

I am building my website by using woocommerce, but i faced the problem of calculating the total amount of the order.
There is a shipping method which is 15% additional fee of the order, and there is a coupon offers $10 discount.
But woocommerce seems like apply the coupon before calculate shipping fee.
For example, the order total amount is $100, the expected result should be (100*1.15) - 10 = 105, but when i tested in the checkout page, the total amount is (100-10) * 1.15 = 103.5.
May I know how to apply the coupon after calculate the shipping fee?
Thank you.
I want to calculate amount correctly in woocommerce, which is apply coupon after calculate the shipping fee.

Add a percentage to the grand total in Woocommerce

In Woocommerce I am using Woocommerce One Page Checkout plugin and I would like to add 10% GST on the Grand Total.
Here is an example (without 10% GST on the Grand Total):
Cart Total: $200
Delivery charges: $20
Grand Total: $220
The result should look (with 10% GST on the Grand Total):
Cart Total: $200
Delivery charges: $20
Grand Total: $242 (including 10% of cart total + 10% on Delivery)
On Products listing page, here is what I did to show total like I want:
$woocommerce->cart->total = $woocommerce->cart->total + number_format(($woocommerce->cart->total * 10) /100, 2);
The problem is that when I place an order, I have a grand total of $220 instead of $242on the payment page.
How can I update the order total here?
Is there any way that we can a 10% GST on the total cart amount in Woocommerce?
Note: I tried to debug it using Network calls and found that Woocommerce is sending cart array that includes all the products with a total and order page might be calculating the total again excluding GST I applied using above code block.
The following code will add 10% to the grand total:
add_filter( 'woocommerce_calculated_total', 'custom_cart_grand_total', 20, 2 );
function custom_cart_grand_total( $total, $cart ) {
return $total * 1.10;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Woocommerce tax rounding issue

I have an item in my WooCommerce shop that costs 24.17 exclusive of Tax. I have a tax rate setup of 20%.
If I add 3 of these products to my cart then it gives me a total including Tax of £87.01 when I would expect to get £87.
24.17 plus 20% is £29.004, why is it not rounding this down to £29.00?
When WooCommerce calculates the tax it first multiples the price without tax by the quantity being ordered. So as far as WooCommerce is concerned, the price to apply tax to is £72.51. It adds 20% to this value. You then get £87.012. It is this final number which is rounded down to £87.01.
To show that mathematically:
24.17 x 3 x (1 + 0.2) = 87.012.
If you want WooCommerce to change the way it calculates that tax you'll have to add the code to your functions.php file. It might look a little like the following:
add_filter( 'woocommerce_get_price_excluding_tax', 'custom_edit_price_function',10,3);
function custom_edit_price_function($price, $qty, $product) {
//calculate price how you like it here.
return $price;
}

WooCommerce shipping price INCLUSIVE tax

In WooCommerce I can only enter the shipping price excluding tax.
How can I make the given price including tax?
The settings to show 'prices including tax' only applies to the products.
So for example if I enter in the settings shipping €1,00
It shows on the checkout €1,21 (0,21 = tax)
It should be €1,00 (incl. 0,21 tax)
(Don't care about the calculation here, it's just an example)
Thanks if anybody has a solution or function for this.
A year after the original question I just hit the same problem and there is not a lot out there to help.
On investigation this turns out to be a problem with the shipping tax calculation from "class-wc-tax.php" which as you can see below hard codes shipping to be tax exclusive.
/**
* Calculate the shipping tax using a passed array of rates.
*
* #param float Price
* #param array Taxation Rate
* #return array
*/
public static function calc_shipping_tax( $price, $rates ) {
$taxes = self::calc_exclusive_tax( $price, $rates );
return apply_filters( 'woocommerce_calc_shipping_tax', $taxes, $price, $rates );
}
It turns out that for some reason that woo have pushed this assumption of shipping being tax exclusive through their down stream tax calculations so you will need to add a couple of filters to your functions.php in order to fix things.
1) The first to calculate the inclusive tax
function yourname_fix_shipping_tax( $taxes, $price, $rates) {
if(wc_prices_include_tax()){
return WC_Tax::calc_inclusive_tax( $price, $rates );
}
return $taxes;
}
add_filter( 'woocommerce_calc_shipping_tax', 'yourname_fix_shipping_tax',10,3);
Careful that you don't forget the 10,3 at the end of add_filter. The 10 is the priority which I have left at the default. The 3 is the number of arguments. You really need this one - the filter displays behavioral problems if you leave it out.
2) The second filter is to fix the totals in downstream calculations.
function yourname_fix_totals( $cart) {
if(wc_prices_include_tax()){
$cart->shipping_total -= $cart->shipping_tax_total;
}
}
add_filter( 'woocommerce_calculate_totals', 'yourname_fix_totals');
Detail for those that worry that this is real money and real taxes we are talking about
The first filter is fairly straight forward - it simply recalculates the tax in the inclusive case.
The second filter is to deal with woo pushing the assumption of exclusive tax further into their tax calculations.
Around line 1396 of 'class-wc-cart.php' we have the following.
// Grand Total - Discounted product prices, discounted tax, shipping cost + tax
$this->total = max( 0, apply_filters( 'woocommerce_calculated_total', round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ), $this ) );
What this wants to do is add the shipping tax to the shipping total which we don't want it to do - and they have hard coded it in......
What we will do is use the conveniently located 'woocommerce_calculate_totals' filter() a few lines above around line 1392 to subtract the shipping tax from the total before it all gets added up using the second filter above.
// Allow plugins to hook and alter totals before final total is calculated
do_action( 'woocommerce_calculate_totals', $this );
In an earlier cut of this post from a few weeks back I was worried that this would be a can of worms - and it is turning out to be just that now that I can test things.
So huge caveat here. Test your final tax totals and subtotals thoroughly - this is no longer possibly a risk. Woo have made the assumption of tax exclusive shipping and have pushed it through their downstream calculations. Unfortunately you will have to pay tax to your respective governments even if there was a bug in you code.
Remember also when testing that when you change from exclusive to inclusive taxes in woocommerce settings that old prices are not affected, so you may need to use clean test data.
With all that said so far it is looking ok for me, but in my scenario I am bypassing large amounts of woo - I am not using any of their front end code which means I may not have found other places impacted by the assumption of tax exclusive shipping.
A possible alternative solution is to manually adjust the pricing for shipping so it becomes VAT exclusive. I.e. if shipping incl 20% tax is 6 EUR, adjust it to 5 EUR. Note that this only works if your tax rate is the same for all shipping destinations.

WooCommerce shipping discount

I have a client that set up a site with WooCommerce and are processing tax exempt customers with a coupon code. The coupon applies a discount to amount using the same percentage as the sales tax essentially offsetting the amount.
the problem I have is they are also charging sales tax on shipping. The coupon will apply the discount to the items in the cart but not shipping. I need to make sure a discount is applied to shipping to offset the tax there is well.
This is my first time looking under the hood of WooCommerce and could use some advice.
My first step has been to start with the woocommerce_before_cart_table hook to get applied coupons and totals. Just not sure were to go from there.
Again, any advice is greatly appreciated.
I should also point out there is no budget or time to go in and do a full tax exemption plugin. (Although I believe I may when I have time)
Filter the shipping rates and remove the taxes for tax exempt buyers.
add_filter( 'woocommerce_package_rates', 'filter_rates_to_remove_taxes', 10, 2 );
public function filter_rates_to_remove_taxes( $rates, $package ) {
// if buyer is tax exempt remove taxes from rates //
return $rates;
}
And do tell client to pay for your services, he surely is out to make money with the site and you should treat this professionaly.

Categories