0

Any one know how to get discount rule and discount from cart? See the pic

enter image description here

Can i get this from external script using cart object? This part of code generate the discount amount section:

public function renderTotal($total, $area = null, $colspan = 1)
{
    //if the code is "discount"
    $code = $total->getCode();

    if ($total->getAs()) {
        $code = $total->getAs();
    }
    return $this->_getTotalRenderer($code)
        ->setTotal($total)
        ->setColspan($colspan)
        ->setRenderingArea(is_null($area) ? -1 : $area)
        ->toHtml();
}

Update

try {
    $product_id = '907'; // Replace id with your product id
    $qty = '1'; // Replace qty with your qty
    $product = Mage::getModel('catalog/product')->load($product_id);
    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $cart->addProduct($product, array('qty' => $qty));
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
    Mage::getSingleton('core/session')->addSuccess('Product added successfully');

    $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();
    //work
    echo $totals['discount']->getValue()."<br>";

    $quote2 = Mage::getSingleton('checkout/session')->getQuote();

foreach ($quote2->getAllItems() as $item){
    //work
    $discountTotal += $item->getDiscountAmount();

}
echo $discountTotal."<br>";

The left question is how to get discount label?

2
  • What exactly you want? discount rule means exact condition ??? Feb 27 '17 at 15:43
  • discount rule name, the discount amount. The selected text of the pic
    – hkguile
    Feb 27 '17 at 15:46
1

To get the different summs for the rules I implemented an observer and save the sums into the item:

<?php


class Project_AddRuleSpecificDiscountToItem_Model_Observer
{
    public function salesruleValidatorProcess(Varien_Event_Observer $event)
    {
        $item = $event->getItem();
        $discount = $item->getDiscountPerRule() ?: [];

        $discount[$event->getRule()->getId()] = array_merge(
            $event->getResult()->getData(), ['qty' => $event->getQty()]
        );

        $item->setDiscountPerRule($discount);
    }
}

And the config.xml part for it:

<frontend>
    <events>
        <salesrule_validator_process>
            <observers>
                <project_addrulespecificdiscounttoitem>
                    <type>singleton</type>
                    <class>project_addrulespecificdiscounttoitem/observer</class>
                    <method>salesruleValidatorProcess</method>
                </project_addrulespecificdiscounttoitem>
            </observers>
        </salesrule_validator_process>
    </events>
</frontend>

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.