====== Debug incorrect totals ======
==== Problem ====
When Gift code is added to cart, some taxes are not deducted from total even if certificate amount is enough.
==== Reason ====
The cause for this issue is the way totals are calculated for cart.
Gift total should be calculated last before grand total and if it is put out of order and is not second to last end result is wrong.
For example if tax is calculated after gift total, it will not be covered by gift.
Reason for this order issue is how Magento orders totals.
The order is defined in config.xml files. For giftcert it is:
ugiftcert/quote_total
ugiftcert/cart_total
subtotal,discount,shipping,tax
grand_total
In the snippet above, ''ugiftcert'' is the code for gift total, it should be calculated after ''subtotal,discount,shipping,tax'' and before ''grand_total''
Similar code is defined for the other totals. Then all configs are merged and sorted by Magento. And during the sorting process is where the issue happens.
If there are race settings like few totals declaring to be ''grand_total'' they can be sorted in order of appearance (it is actually more complicated but lets assume it is this simple).
Usually cause for this misalignment is addition of a new total.
==== Solution ====
To fix the sorting, first we need to find which total causes the misalignment.
To do that we need to log totals in order Magento sees them.
For this we need to add some logging to a core file.
**/app/code/core/Mage/Sales/Model/Quote/Address.php** find ''Mage_Sales_Model_Quote_Address::collectTotals()'' method. It looks like:
public function collectTotals()
{
foreach ($this->getTotalCollector()->getCollectors() as $model) {
$model->collect($this);
}
return $this;
}
To find order of totals, we change above code as follows:
public function collectTotals()
{
Mage::log(__METHOD__, null, 'gc.log', true);
foreach ($this->getTotalCollector()->getCollectors() as $model) {
Mage::log($model->getCode(), null, 'gc.log', true);
$model->collect($this);
}
return $this;
}
This will log codes of all totals in **var/log/gc.log** file. You can then send us the file to inspect it.
Or you can check it yourself. Find if any totals appear between ''ugiftcert'' and ''grand_total'' and if there are any non default totals.
When potential problems are identified, we need to try and fix them. We do that by editing the config.xml files in the total declaration part.
Unfortunately there is no guaranteed fix, and it is trial and error process.
Disable config cache in admin and then edit the sections for offending totals.
For example, we have change **/app/code/community/Unirgy/Giftcert/etc/config.xml** to be like:
ugiftcert/quote_total
ugiftcert/cart_total
subtotal,discount,shipping,tax
grand_total
shipping,weee
grand_total
grand_total
You can try fixes for any suspicious totals in the same file.
Since this could be tricky to figure out by your selves, you can gather some preliminary data and send it to us along with FTP access to try to fix the issue for you.