I want to set the Discount (say $10) per Item dynamically, after a custom button is pressed, in the Checkout Cart page. I have checked some of the coding of Magento, to find that:
- Mainly the "
Mage_Sales_Model_Quote_Address
" class is affected always, when anybody (any Magento code) is talking about discount. - There are 2 instances of "
Mage_Sales_Model_Quote_Address
" - one for "billing" address type & the other for "shipping" address type, but the latter one is mainly used for coupon code related discounts.
After checking these area, I went on to write some code, targeting the "shipping" address type of "Mage_Sales_Model_Quote_Address
", as:-
$cart = Mage::getSingleton('checkout/cart');
$objShippingAddress = $cart->getQuote()->getShippingAddress();
$discountAmount = 10;
$objShippingAddress->setDiscountDescription('any description');
$objShippingAddress->addTotal(array(
'code' => 'discount',
'title' => "Custom Discount",
'value' => -$discountAmount,
));
$totalDiscountAmount = $discountAmount;
$subtotalWithDiscount = $discountAmount;
$baseTotalDiscountAmount = $discountAmount;
$baseSubtotalWithDiscount = $discountAmount;
$objShippingAddress->setDiscountAmount($totalDiscountAmount);
$objShippingAddress->setSubtotalWithDiscount($subtotalWithDiscount);
$objShippingAddress->setBaseDiscountAmount($baseTotalDiscountAmount);
$objShippingAddress->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
$objShippingAddress->setGrandTotal($objShippingAddress->getGrandTotal() - $objShippingAddress->getDiscountAmount());
$objShippingAddress->setBaseGrandTotal($objShippingAddress->getBaseGrandTotal() - $objShippingAddress->getBaseDiscountAmount());
But still I don't get any line in the "totals
" section of my checkout cart page & in the order review section of checkout one-page.