Subtract tax from a price with tax in OpenCart? - php

How do I subtract all taxes from a price with tax in OpenCart?
In the example below I use the default "Taxable goods" tax setting from OpenCart, which is 20% VAT + $2,00 Eco-Tax.
$number = 20.80
// Get $number tax:
$tax = $this->tax->getTax( $number , $product_info['tax_class_id'] , $this->config->get('config_tax') );
// Subtract tax from total price:
$result = $this->currency->format( ( $number - $tax ) , $this->session->data['currency'] );
This returns an incorrect value of $14,64 because it calculates the tax on $number (20,80), which already is a price with tax. The correct price for $20,80 without tax should be $15,67
This should be the formula in this case:
(20.80 - 2.00) / 120 * 100 = 15.6667
Is there any way to subtract all taxes from a price that already has tax included?

$taxRate = 20;
$gross = 150;
$divisor = (100 + $taxRate) / 100;
$net = round( $gross / $divisor, 2);
$tax = round( $gross - $net, 2 );
Echo "Gross was $gross - Net is $net Tax is $tax";
RESULT with 20% tax
Gross was 150 - Net is 133.33 Tax is 16.67
RESULT with 15% tax
Gross was 150 - Net is 130.43 Tax is 19.57
Try a few dry runs and then check with this code

if your tax for example is 21% and you have product with tax=120.. Without tax will be: 120/1.21.
so if Tax rate:
$tax_rate = 21;
product price:
$price = 120;
Price without tax:
$price_without_tax = $price/($tax_rate/100 +1);

Related

calculate discount for total amount multiplied by 100

I'm having trouble calculating the discount of total order amount of products that is multiplied by 100.
In my database I'm saving the product price multiplied by 100 e.g.: $price = 33.5 * 100 I'm doing this to prevent wrong calculation of the decimal point .5 when retrieving the data and adding those to other products.
So I have this for now
$test_product_price = 10000 // actual price is 100 because it's save multiplied by 100
$quantity = 3;
$discount = 10.5%;
$order_amount = $test_product_price * 3 // will produce 30000 but actual price is 300 when divided by 100
Now what I want is deduct the $discount which is 10.5%
for actual amount this is working fine
$discounted_amount = 300 - (300 * (10.5 / 100)); // will produce 268.5
but how can I do that on my end that is my amounts are multiplied by 100
Possibly a bit long winded but accurate;
<?php
$amount = 100;
$number = 3;
$percent = 10.5;
$subtotal = $amount * $number;
$discount = ($percent / 100) * $subtotal;
$total = $subtotal - $discount;
$total = number_format((float)$total, 2, '.', '');
echo $total;
?>

Add an additional cost to flat rate shipping each 3 items in Woocommerce

I'm running a woocommerce shop and using a Flat Rate shipping $15. I have written a formula to add $1.25 for each additional item.
13.50 + ( 1.25 * [qty])
Sipping "flat rate settings | $1.25 for Additional Each Item:
But I want to add this cost $1.25 for every 3 items. I mean 3, 6, 9, 12 and so on...
Can anyone tell me how to do this? Any help is appreciated.
Updated (2021)
The following code will add an additional cost to flat rate shipping method each 3 items (3, 6, 9 …).
You will need to change your shipping cost with a simple initial cost instead of your formula.
You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.
The code (where you will set your additional shipping cost):
add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 10, 2);
function shipping_additional_cost_each_three_items( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE set your additional shipping cost
$additional_cost = 1.25;
$items_count = 0; // Initializing
$each_items = 3; // Number of items (for additional cost)
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ) {
$items_count = += $cart_item['quantity']; // Count cart items for current shipping package
}
if ( $items_count >= $each_items ) {
// Loop through the shipping taxes array
foreach ( $rates as $rate_key => $rate ){
// Targetting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$initial_cost = $new_cost = $rate->cost;
$has_taxes = false; // Initializing
$taxes = array(); // Initializing
// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i+ = $each_items){
$new_cost += $additional_cost;
}
$rates[$rate_key]->cost = $new_cost; // Set the new cost
// Taxes rate cost (if any) - Loop through taxes array (as they can be many)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $tax;
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax changes
}
}
// set array of shipping tax cost
if( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Don't forget to disable "Enable debug mode" option in shipping settings.
Answer based on your 2nd comment:
you will replace this block:
// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
$new_cost += $additional_cost;
}
by the following:
// Adding to cost an additional fixed cost for the 2nd item
if($items_count >= 2){
$new_cost += 6.21;
}
// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
$new_cost += $additional_cost;
}
<?php
$number = 13;
$all_number = 1;
for($i=1;$i<=13;$i++){
if ($i % 3 == 0){
$all_number = $all_number + 1;
}
}
$price_new_data = $old_price*$all_number;
$price = 13.50+($price_new_data*$product_quenty);
?>

PHP - apply discount to items divisible by 2

I am working with some code which will apply a discount to an item should the item be bought in multiples that are dividable by 2, this works if I put in any number dividable by 2.
The problem is if the user inputs say 3, I would still like the discount to be applied as they have bought 2 together plus another.
What would be the best approach to tackling this? This is the code I'm working with:
if($price == 11.99 && $each_item['quantity'] % 2 == 0)
{
$toAdd = 3.98 * ($each_item['quantity'] / 2);
$discount += $toAdd;
$priceTotal = $price * $each_item['quantity'];
$priceTotal -= $discount;
$finalDiscount += $discount;
}
Divide the quantity by 2, and round it down to an integer to get the number of discounts to apply.
if ($price == 11.99) {
$to_add = 3.98 * floor($each_item['quanity']/2);
$discount += $toAdd;
$priceTotal = $price * $each_item['quantity'];
$priceTotal -= $discount;
$finalDiscount += $discount;
}

Calculate only value of vat

I trying to calculate but can't get this done, if someone can help
I have the total of the order and the vat at 23%
$vat = 23;
$total = 6.00;
For add the vat to total I will do
$total_with_vat = $total * $vat;
that will be 7.40
I now the total is 6.00, but how can I get the 1.40 of the vat
I need this to make some calculations. Looks simple but I'am not getting only to do it.
Thanks
Pretty simple
$vat = $total * $vat / 100.0;
// 1.38
If you'd like to guarantee 2 digits of precision, you can use round
$vat = round($vat, 2);
Make it into a reusable function
function calculateVat($amount, $vat=23) {
return round($amount * $vat / 100.0, 2);
}
$amount = 6.00;
$vat = calculateVat($amount);
// 1.38
If VAT is a different amount, you can specify it as a second argument
$amount = 6.00;
$vat = calculateVat($amount, 30);
// 1.8

PHP Sales Tax Calculating

So I can't quite figure out what is wrong here. I am trying to calculate Sales Tax into a grand total. I'm trying to get two variables here:
A. The total amount of sales tax that is being charged (i.e. $0.61 for sales tax)
B. The Grand total including the item price times the quantity plus the sales tax.
My script is as below, The way I have this set up right now it only adds a penny to the sub total. (instead of $7.00 the total is $7.01)
If the sub total after each item is $7.00 and the tax rate is 8.650% then the tax total should be $0.61 and the grand total should be $7.61 but it's making it $7.01 instead.
public function invoice_totals($invoice_id)
{
$query = $this->CI->db->select('*');
$query = $this->CI->db->from('invoice_to_items');
$query = $this->CI->db->where('invoice_id', $invoice_id);
$query = $this->CI->db->join('items', 'items.item_id = invoice_to_items.item_id');
$query = $this->CI->db->get();
$items = $query->result();
$sub_total = '0.00';
$grand_total = '0.00';
$tax_rate = '0.0865';
$tax_total = '0.00';
foreach($items as $item)
{
$sub_total = $sub_total + ($item->item_price*$item->item_qty);
$tax_total = $tax_total + ($sub_total * $tax_rate) / 100;
}
$grand_total = $grand_total + $sub_total + $tax_total;
return array(
'sub_total' => number_format($sub_total,2),
'tax_total' => number_format($tax_total, 2),
'grand_total' => number_format($grand_total,2),
);
}
The main line I am concerned with in this question is:
$tax_total = $tax_total + ($sub_total * $tax_rate) / 100;
You're applying your taxes to your subtotal on EVERY iteration of the loop.
let's say there's 3 items: $5, $15, $40
loop #1:
subtotal = 0 + ($5 * 1) = $5
total = 0 + ($5 + 8.65%) = $5.43
loop #2: $5.43 + ($15 * 1) = $20.43
total = $5.43 + ($20.43 + 8.65%) = etc...
etc...
your #2 item has now double-taxed the first item, and your third item will be TRIPLE taxing the first item and DOUBLE taxing the second item, etc...
As well, your tax rate value is ALREADY a decimal (0.0865), yet you're doing a /100 division as if you had $tax_rate = 8.65. So effectively your tax rate is really 0.0865%
Your loop should be:
$taxrate = 8.65; // % value
foreach($items as $item) {
$item_sub = $items->quantity * $items->price;
$item_total = $item_sub * ($taxrate / 100);
$grand_total = $grand_total + $item_total;
}

Categories