Eli Y
Eli Y

Reputation: 907

magento customize quote collectTotals to show the updated totals

hey i'm implementing a custom discount system since magento discount system does not feet my requirements so i'm trying to apply a discount on an Mage_Sales_Model_Quote_Item I've read some and I've found the following function setOriginalCustomPrice the thing is that it applies on the item, and if the user changes the quantity he will get the discount on the item with the new quantity, so i'm trying to use a different method addOption on the item and only on the cart page show the calculations based on the quantity in the option value

$item->addOption(array('code'=>'promo','value' => serialize(['amount'=>10,'qty'=>1])));

and in the cart page

$promo = $item->getOptionByCode('promo');
echo '<div class="orig-price">'.$item->getOriginalPrice().'</div>';
echo '<div class="new-price">'.$item->getOriginalPrice() - ($promo['amount'] * $promo['qty']).'</div>';

the problem is that it does'nt actually apply the new price on the product, so i want to customize Mage_Sales_Model_Quote->collectTotals() to show my discounts and send it to the admin back-end when order is completed

how can i achieve that? thanks in advance

Upvotes: 0

Views: 3778

Answers (1)

Alexei Yerofeyev
Alexei Yerofeyev

Reputation: 2103

I think there is a fundamental flaw in your approach. I'm not sure what you don't like in standard discounts, and what you can't achieve with catalog or shopping cart rules, but what you're trying to do definitely breaks these features (along with my heart).

However, if you're sure about what you're trying to do, then don't customize Mage_Sales_Model_Quote->collectTotals().

This function just... well, it collects all totals: subtotal, shipping, discount, etc. And it looks like you're changing only price output, but Magento itself doesn't know anything about it. So if you want to let Magento know that you're changing the item price, you have to either add your own total, or change one of the existing totals. After that Magento will do everything else. Since after your changes Magento outputs already calculated price instead of original one, it may be strange for customer to see the original price in the cart and the additional discount total. So it looks like you will have to change subtotal total.

To do that you have to rewrite Mage_Sales_Model_Quote_Address_Total_Subtotal class in your extension and insert your calculation in _initItem() method. Around line 111 in the original file you will see the code:

$item->setPrice($finalPrice)
     ->setBaseOriginalPrice($finalPrice);

And this is where Magento sets price for the item, so you can insert your calculations and change $finalPrice before that. If you have virtual products, you will have to change collect() method too.

Upvotes: 2

Related Questions