Reputation: 1805
In open cart I was building a module and I needed to know how price calculations are done, and I came across this code
$price = $this->currency->format($this->tax->calculate($result['price'],
$result['tax_class_id'], $this->config->get('config_tax')));
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate(
$result['special'],
$result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ?
$result['special'] : $result['price']);
} else {
$tax = false;
}
Actually I didn't get the idea what really happening here as I can see there is really no difference between the $price
and the $special
and the $tax
but there should be a reason why it is implemented this way.
I am sure i am missing something here any one explain to me how the price calculation is done in opencart?
Upvotes: 1
Views: 9053
Reputation: 15151
Just a quick note for Mr Trick's answer. The three parameters of $this->tax->calculate() are
$value, $tax_class_id, $calculate = true
The third of which is not necessary, but in the original code, it gets the global config value of whether or not tax should be applied (since you can disable it in the settings). It's worth also noting that the tax object has changed between versions (I think it was 1.5.1.2 that it changed last) so if you're wanting to make it backward compatible, it's something to consider
Upvotes: 2
Reputation: 1957
From reading the source, this is what I understand: $price
, $special
and $tax
are variables that are passed to the view to display.
$price = $this->currency->format($this->tax->calculate($result['price'],
$result['tax_class_id'], $this->config->get('config_tax')));
Every item has a price, so $price
is always set. $price
is ; the base price, with the applicable tax classes and tax applied.
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate(
$result['special'],
$result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
An item might be on special. If it is, then $special
is set to the base special price, with the same set of tax calculations applied to it. (so that the view code can display both the original and the SPECIAL! price side-by-side)
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ?
$result['special'] : $result['price']);
} else {
$tax = false;
}
Not all installations have a tax configured. If it is, then $tax
is set to be the base or base special price. (so that the view code can display how much the item cost before tax. (it's a little bit illogical that $tax
is the price without any tax on it)
Make sense? If you need to know more about how price is calculated, look more closely at tax->calculate()
. Otherwise it's
$taxed_price = $special ? $special : $price;
$untaxed_price = (float)$result['special'] ? $result['special'] : $result['price'];
Upvotes: 4