1

To add a discount amount to cart total, I am using function collect() and fetch().

Namespace_Module_Model_Sales_Quote_Address_Total_Module Mage_Sales_Model_Quote_Address_Total_Abstract{



        public function collect(Mage_Sales_Model_Quote_Address $address)
        {
            parent::collect($address);

            $this->_setAmount(0);
            $this->_setBaseAmount(0);

            $items = $this->_getAddressItems($address);
            if (!count($items)) {
                return $this; //this makes only address type shipping to come through
            }



    public function fetch(Mage_Sales_Model_Quote_Address $address)
        {
            $amt = $address->getDiscountAmount();

I am using ajax to load the cart total block and show discount amount. The issue is, it displays the discount amount text and function fetch() is used for display purposes. but the cart total deductions are not happening as function collect() is not being called in ajax.

Is it possible to perform same operations in my controller that is being performed in Mage_Sales_Model_Quote_Address ?

If I reload the page, I can see the applied discount and proper deductions but not on ajax click.

Please Help.

1

In your controller call:

$quote->setTotalsCollectedFlag(false)
$quote->collectTotals();

The collector that you have written is called every time you call collectTotals on quote with the totals collected flag set to false.

The collector should look like below:

public function collect(Mage_Sales_Model_Quote_Address $address)
{
    $quote = $address->getQuote();


    if ($address->getAddressType() == 'billing' && !$quote->isVirtual())               return $this;

    $address->setDiscountAmount($address->getDiscountAmount() - $customDiscount);

    return $this;
}

One word of caution through - Discounts are maintained at item level and not at address level. So you may want to change your approach and involve a salesrule somehow.

  • Is $quote equal to $quote = Mage::getSingleton('checkout/session')->getQuote(); ? Also how to set data on $address ? – Jordan Aug 26 '14 at 10:22
  • Yes, correct for getting the quote. Will edit in answer for the address collector. – Paras Sood Aug 26 '14 at 10:27
  • $quote->collectTotals(); will call function collect(Mage_Sales_Model_Quote_Address $address) but how $quote->setTotalsCollectedFlag(false); works and what if we set it to true ? – Jordan Aug 26 '14 at 11:03
  • $quote->collectTotals() one the first line checks for the totals_collected_flat, if it is set to true it returns without doing anything. Look at the definition : Mage_Sales_Model_Quote::collectTotals() – Paras Sood Aug 26 '14 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.