Woocommerce custom tax function - php

I am looking to add a very specific tax calculation to the cart and "invoice plugin" as well. Flat -+ percentage doesn't work since this is a very specific case. I just have no idea how to go about it and all the resources online seem to be about woocommerce inbuilt tax classes, which are flat rates.
Lets define the variables:
Final price = X (in this case it is 100$)
Final price is always known, since its added while creating the product in woocommerce
Subtotal Tax = Y (it's 25% of X)
Subtotal Tax - Actual Tax = Z (it's 21% tax calculation of Y)
Actual Tax amount = M (y-z)
So if final price is X = 100.00
Y = 0.25 * 100 = 25.00
Z = 25/1.21 = 20.66
M = 25.00 - 20.66 = 4.34 (rounded up to 2 decimal places)
In the cart i need to output this:
Subtotal: (X)
Price without tax: (X - Y)
Price applicable with special tax: (Z)
Special tax amount: (M)
Total Sum: (X)
I found this code to add extra fees (taxes) to the cart, but nothing that would help me achieve desired result.
// Add CUSTOM TAX
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
$percent = 25;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'Custom Tax', 'woocommerce')." ($percent%)", $surcharge, false );
}
I can do the calculation myself, i just need to know how to add multiple new lines to the output so its displayed in the cart totals and later in the invoice plugin.

Related

Php TAX substract & add back

I have a shop where the client enters the product price with the tax in his country. When i save it i need to remove the tax and keep only the base.
When i calculate the tax back for the clients from the same country in some cases the total price will be different with 0.01 , 0.03 then the original price.
I tried to calculate with 2 to 4 digits but it never works on all cases.
Ex: price with tax = 157, tax is 7.7% => base = 145.78 and tax amount 11.22
When i calculate the tax to the previous price base i get this:
Base: 145.78 , tax is 7.7% => 11.23 = price is 157.01
Php code i use:
$baseprice = round($fullprice / (1+($taxperc/100)),2);
On the frontend:
$tax = round($baseprice * ( $taxperc / 100 ),2);
$total = $tax + $baseprice;
Anyone have a suggestion about how to fix this ?
Thanks!
Update: after some research I found a solution is to use round with PHP_ROUND_HALF_EVEN
Update2: first solution doesnt work. 1-9 works fine, 10 -tax + tax =10.01
Today's solution would be to work on the back with full number of digits ( for base price, tax and total ) and only when display for user to round it. :)
Seems ( not 100% sure yet, i need to test more ) a solution is to use the banker's rounding PHP_ROUND_HALF_EVEN
$baseprice = round($fullprice / (1+($taxperc/100)),2 , PHP_ROUND_HALF_EVEN);
$tax = round($baseprice * ( $taxperc / 100 ),2 , PHP_ROUND_HALF_EVEN);
$total = $tax + $baseprice;

How to calculate product price after adding payment gateway commission to it?

We have an item to sell worth around $100. There is a 5% tax applicable to it. A third party payment gateway takes a commission of 3% of total amount given to gateway (ie., 3% of 100+5%).
Since its not possible to charge 3% payment gateway commission customer, we hide this extra commission under item price.
So the price should be increased from 100 to an "X" amount.
(100 + 5% tax) + 3% Commission = (X + 5% Tax) ;
Please note when increase amount from X + 5% Tax, the commission also increases.
(100 + 5%) + 3% = (100 + 5) + 3.15 = 108.15
If we sent 108.15 to gateway, it charge 3.255 on the amount 108.15, which means 0.105 additional. ie. We received a lesser amount after deducting gateway commission (104.895).
I need to isolate the item price which will not result in an additional charge to company.
$tax = 5 ; //5%
$itemAmount = 100 ;
$priceWithTax = $itemAmount + ($itemAmount * 5/100) ;
$commission = 3 ; //3%
//We sent 105 to gateway.. which results 105 to customer and 3.15 to company.
$priceWithTaxCommission = $priceWithTax /* or X */ + ($priceWithTax * $commission/100) ;
$ToGateway = $priceWithTax + ($priceWithTax* 3/100) ;
//Results 108.15, If we sent 108.15 to gateway it again charge 3% on 108.15. Which is wrong.
How to find product price after including payment gateway commission ?
Don't think in the term of "add", think in the term of "multiply":
Multiply your worth with factor f:
f = 1/0.97
100 * (1/0.97) * 1.05 = ~108.25 (shop-price including taxes)
108.25 * 0.03 = ~3.25 (commision)
-> 105.00 (what's left and that is 100 + 5% taxes).
You can also parameterize factor f:
f = 100 / (100 - commision)
Please have a look at my answer and let me know if you want to consider the taxes of the commision-part in your shop-price. You can do this but in my opinion this would be wrong from a accountance-view.
In this case use from this code:
$mainPrice = 100;
$tax = 5;
$commission = 3;
$priceWithTax = $mainPrice + ($mainPrice * ($tax / 100));
echo $priceWithTax.'<hr/>';
$priceWithTaxCommission = $priceWithTax + ($priceWithTax * ($commission / 100));
echo $priceWithTaxCommission.'<hr>';
$x = $priceWithTaxCommission / (1+($tax / 100));
echo 'product price is=>'.$x.'<hr/>';
echo $x + ($x * ($tax / 100));
this code return as following:
price with tax 105
price with tax and commission 108.15
product price is=>103
product price with commission 108.15

Rounding a VAT commission in PHP

I want to achieve following: putting a commission with VAT on top but rounding the commission so commission + VAT have a round number. In PHP.
Lets say we have this:
50 Euro original Price
12 % + 4 Euro comission = 10 Euro
19 % VAT on the commission only(!)
61,19 total price
But what I want to have is
61 Euro Total price
How do I calculate it, that the commission itself decreases and both, commission and VAT make a round (no decimals) number?
$base = 50.00;
$comission = $base * 0.12 + 4.00;
$vat = $comission * 0.19;
$total = $base + $comission + $vat;
Thats what I got. Hope you can tell me the clue!
Cheers

PHP: Calculating percentage tax applied to a product

I've got a significant problem in working out the percentage tax applied to a product, due to the rounding.
For example:
If you have a product which is £1.00 including 20% tax the break down would be:
0.83 ex tax
0.17 tax
1.00 total
However, if you work out the percentage increase:
round( (( ( 1 - 0.83 ) / 0.83 ) * 100), 2);
The answer is 20.48, because the actual price ex VAT is 0.8333333333
Therefore if you calculate:
round( (( ( 1 - 0.8333333333 ) /0.8333333333 ) * 100), 2);
You get the correct answer of 20.
In this case it would obviously work to round the 20.48 down to 20, but thats not a solution because some tax rates are to 2 decimal places so the assumption can't be made that you can just round the tax rate.
Am I missing something or is this impossible without knowing the original tax percentage?
0.17 is not 20% of 0.83, so your basic assumption is inaccurate( is rounded :P ).
Don't round money, calculate it without all that rounding and display rounded if need be. That avoids having to loose the precision in calculations.
A simple test will demonstrate
$price=0.8333333333;
$taxRate=21.25;
$tax=$price*$taxRate/100;
$total=$price+$tax;
$calculatedTaxRate=(($total-$price)/$price)*100; // 21.25
Since we didn't round anywhere, we can reverse engineer the tax rate always down to the dot.
Try with 20%
$price=0.8333333333;
$taxRate=20;
$tax=$price*$taxRate/100;
$total=$price+$tax;
$calculatedTaxRate=(($total-$price)/$price)*100; // 20
Wouldn't it something like:
17% VAT
85.47 taxless
85.47x0.17 = 14.53
Total: 100
So 100/1.17 = 85.47
My little-and-dirty version:
$taxrate=22; // 22%
$price=100;
$priceMinusTax=($price/(($taxrate/100)+1));
As long as you are dealing with low precision numbers, you will get a low precision answer. If you must show low precision numbers, you can round them when you show them to the user in the view, but save them as high precision numbers in the database.
Assuming your total price is £1 and tax rate is 20%:
$vatDecimal = (float) 1 + (20 / 100);
$priceExclVAT = round((1.00 / $vatDecimal), 2);
$priceDisplay = number_format($priceExclVAT, 2, ',', '.');
echo $priceDisplay;
The user enters the total amount and tax. Sometime its come form database, we can also use this code.
$actualPrice = "";
$total = 1000;//User Entry Total Amount
$gst = 18;//User Entry GST Tax %
$calculateTax = 100+$gst;
$calculateAmount = $total*100;
$actualPrice = $calculateAmount/$calculateTax;
echo $actualPrice = round($actualPrice,2);
echo $gstAmount = round($total-$actualPrice,2);

I want to fix the shipping price magento based on postcode and weight?

I have to fix the shipping price in magento based on postcode and weight for example the shipping price for specific postcode is stable upto 20 kg if its exits 20 kg i have to increase the shipping price like 1.30 euros for every kg.how can do that?. I hv already looked the table rates but i think it wnt suit my scenario. Can anyone pls help me.thanks
You can use a function like:
function calculateShippingFee($parcelWeight, $standardShippingFee){
$maxWeight = 20; //Max weight of parcel before additional cost
$overWeightFee = 1.30; //Fee per kg over weight
$additionalFee = 0; //Initialise additional fee
if($parcelWeight > $maxWeight){
$amountOver = ceil($parcelWeight) - $maxWeight; //Amount over the max weight
$additionalFee = $amountOver * $overWeightFee; //Additional fee to be charged
}
return $standardShippingFee + $additionalFee;
}
Which will return the calculated shipping fee. All you have to do is feed it the $parcelWeight and $standardShippingFee for the postcode, like:
$shippingFee = calculateShippingFee(25, 5.30); //Weight == 25kg, Fee == €5.30
Example outputs:
echo calculateShippingFee(19, 10); // Outputs: 10
echo calculateShippingFee(20, 10); // Outputs: 10
echo calculateShippingFee(25, 10); // Outputs: 16.5
echo calculateShippingFee(24.3, 10); // Outputs: 16.5
Function with changing over weight fee
function calculateShippingFee($parcelWeight, $standardShippingFee, $overWeightFee){
$maxWeight = 20; //Max weight of parcel before additional cost
$additionalFee = 0; //Initialise additional fee
if($parcelWeight > $maxWeight){
$amountOver = ceil($parcelWeight) - $maxWeight; //Amount over the max weight
$additionalFee = $amountOver * $overWeightFee; //Additional fee to be charged
}
return $standardShippingFee + $additionalFee;
}

Categories