I'm having a very hard time collecting the right totals when creating an order by code (magento 1.9, php5.4). I'm creating a quote as you noramlly would, added products and shipping/billing address, and now I'm collection the subtotal/shipping/taxes/grandtotal, and im not getting the right calculations.
For the example below, I have the following:
Product Price is 100
Taxes on Product Price is 25
Shipping Amount is 10
Shipping Taxes is 2.5
Grand total should be 137.5
When when executing $quote->collectTotals();
to get the order grand total, im always getting 110
for $quote->getGrandTotal()
or $quote->getShippingAddress()->getGrandTotal()
. I should be getting 137.5
.
So I google around and did my homework, and apparently there was a problem in the order of the collection, so magento was collecting using the following order:
tax_shipping
nominal
subtotal
freeshipping
tax_subtotal
weee
shipping
discount
grand_total
msrp
tax
and the following total amounts were calculated:
So it looked like grand_total
was calculated before adding the tax, and this is why I was getting 110
instead of 137.5
, which makes sense, since all what the grand total class does is to sum up the total:
class Mage_Sales_Model_Quote_Address_Total_Grand extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
/**
* Collect grand total address amount
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Mage_Sales_Model_Quote_Address_Total_Grand
*/
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$grandTotal = $address->getGrandTotal();
$baseGrandTotal = $address->getBaseGrandTotal();
$store = $address->getQuote()->getStore();
$totals = array_sum($address->getAllTotalAmounts());
$totals = $store->roundPrice($totals);
$baseTotals = array_sum($address->getAllBaseTotalAmounts());
$baseTotals = $store->roundPrice($baseTotals);
$address->setGrandTotal($grandTotal+$totals);
$address->setBaseGrandTotal($baseGrandTotal+$baseTotals);
return $this;
}
So I though if I changed the order of the collection, by moving the grand_total collection after the tax collection, this would solve my problem, so used i overrode the order using this simple extension https://github.com/hartmut-co-uk/magento-php7-totals-fix, after all it looked like a lot of people are facing the same issue with PHP7 and PHP5.5, although im using PHP5.4, i thought i would give it a shot, considering how desperate i am.
Now things are a bit different, the collection total changed to:
nominal
freeshipping
tax_subtotal
msrp
subtotal
weee
shipping
tax_shipping
discount
tax
grand_total
So I though all my problem went away, but for some reason, now the tax are not calculated the right way, I'm getting 2.5
for the tax instead of 27.5
I know the post is super long and can be hard to follow, but hopefully I was able to explain the problem. So the first question is:
- Why grand_total collector is not called in the right order in the first place.
- I have no idea what to do next. Suggestions are strongly welcomed :D.