3
votes

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.

Anybody please help. I know it can be doable.

Thanks to all in advance.

1
Is there no one who can try this one? It's tough I admit, but please there must be somebody who have tried this in his Magento's stint... - Knowledge Craving
check Ivan's answer: stackoverflow.com/questions/4877413/… it worked for me. - OSdave
@OSdave is right. It is the best way to add custom fee or discount. - Ivan Chepurnyi
@KnowledgeCraving can you shed some light on where/when is this code snippet called? - Seth Malaki

1 Answers

0
votes

We had a request to setup discount tiers. Since we could not do them using the current discount model, we made modifications to the couponPostAction in the CartController. We setup a dummy coupon and was able to, based on other information about the customer, offer a tiered discount scheme with that coupon.