5

I have a weird problem. I have developed a module which adds a line to totals according to some value in database. But in my module model (which is inherited from Mage_Sales_Model_Quote_Address_Total_Abstract) when I call

$address->getSubtotal()

or

$address->getGrandTotal()

or any other totals method, I get zero (0) returned. But in phpmyadmin, I see that those values are not zero. Any other column except these totals columns returns their correct value (i.e, getAddressId() return the ID, getAddressType returns "shipping" etc.)

What may be the problem, any idea? Thanks ---------- EDIT ----------- OK, After @Alan Storm's comment, I see that I should be more clear. I'm trying to develop an instalment module. I will set instalment fees (changing according to months count) in the admin, and I will add this fee to cart total at the checkout.

This is my collect method,

public function collect(Mage_Sales_Model_Quote_Address $address)
{

$address->setInstalmentCount(2); //instalment count is hardcoded as 2 for debugging

$paymentMethod = Mage::app()->getFrontController()->getRequest()->getParam('payment');
$paymentMethod = Mage::app()->getStore()->isAdmin() && isset($paymentMethod['method']) ? $paymentMethod['method'] : null;
if ($paymentMethod != 'oos' && (!count($address->getQuote()->getPaymentsCollection()) || !$address->getQuote()->getPayment()->hasMethodInstance())){            
    return $this;
}

$paymentMethod = $address->getQuote()->getPayment()->getMethodInstance();

if ($paymentMethod->getCode() != 'oos') {            
    return $this;
}

$items = $address->getAllItems();
if (!count($items)) {
    return $this;
}

$baseTotal = $address->getBaseGrandTotal();   // THIS ALWAYS RETURNS ZERO

// adress is the reference for grand total
$quote = $address->getQuote();
$store = $quote->getStore();

$fee_perc = $oosconfig['inst' . round($address->getInstalmentCount())]; // get the setting from admin
$ins_fee = $store->convertPrice($baseTotal*$fee_perc/100.0, false); // calculate the fee    

$baseTotal += $ins_fee; // add to totals

$address->setInstalmentFee($ins_fee);

// update totals
$address->setBaseGrandTotal($baseTotal);
$address->setGrandTotal($store->convertPrice($baseTotal, false));    

return $this;   

}

------ EDIT2 ------

OK guys, I have figured it out! The problem was with my config.xml I should have added

<after>grand_total</after>

Since it is absent, it was first collecting my module's total while subtotal and grand_total were not calculated yet. Because of this, they were coming as zeros.

Thank you though!

Toto
  • 89,455
  • 62
  • 89
  • 125
UnfoX
  • 135
  • 1
  • 12
  • It's not clear what you're trying to do here with the Address quote object, which makes providing an answer difficult. This class is not for fetching values from the database. – Alana Storm Dec 26 '11 at 19:40
  • I have edited the question accordig to your comment. – UnfoX Dec 26 '11 at 19:50
  • 2
    OK guys, I have figured it out! The problem was with my config.xml I should have added grand_total Since it is absent, it was first collecting my module's total while subtotal and grand_total were not calculated yet. Because of this, they were coming as zeros. Thank you though! – UnfoX Dec 26 '11 at 21:18
  • Be sure to add your answer below in the answer space. People will vote it up, you'll get more rep, and people with higher rep tend to get future questions answered more quickly. – Alana Storm Dec 26 '11 at 21:38
  • it does not allow me to do so – UnfoX Dec 26 '11 at 23:17

2 Answers2

7

Adding the OPs "Self Help Desk" answer. If you like it, be sure to up vote the original question.

OK guys, I have figured it out! The problem was with my config.xml I should have added

<after>grand_total</after>

Since it is absent, it was first collecting my module's total while subtotal and grand_total were not calculated yet. Because of this, they were coming as zeros.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
3

Try this it works...

 public function collect(Mage_Sales_Model_Quote_Address $address)
    {

    $items = $quote->getAllItems();
    $subtotal = 0;
    foreach ($items as $item){
        $subtotal += $item->getRowTotalInclTax();
    Mage::log($subtotal);
    }

}
Amaresh Tiwari
  • 947
  • 13
  • 36