开发者

How to set Dynamic Discount on each Cart Item programmatically?

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);
$objSh开发者_Python百科ippingAddress->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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜