Magento 1.9 – How to Add Custom Discount Amount for Specific Quote Items

discountmagento-1.9quoteitem

What I know.

What I have seen on my searches till now is that we can add custom discount to the quote and split the discount among the quote Items based on ratio calculated.

What I want.

But I want to give specific discount on some line Items(quote items) , say a Gift Item included with a product. As it's a gift Item, I want to give the line item 100% discount, such that my row total will be zero and subtotal have the grand total will have the subtotal – discount amount.

The same line item added individually will be a regular product with no discount.

The Problem

I'm not able to give discount amount to the quote item with out making issue existing calculation.

How can I achieve such a scenario in Magento 1.x programatically? Any Ideas? I have tried using the sales_quote_collect_totals_after and sales_quote_address_discount_item observers with no luck.

Best Answer

Find the Extension for free product.

You have to do changes for your requirement.

Make changes in C4B_Freeproduct_Model_Observer function salesruleValidatorProcess

You have to retrieve sku of that product which you want to add into cart as free product (Way you are retrieving in your curent observer) . This function runs for the all products in the cart.

    public function salesruleValidatorProcess(Varien_Event_Observer $observer)
    {

        /* @var $quote Mage_Sales_Model_Quote */
        $quote = $observer->getEvent()->getQuote();

        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getEvent()->getItem();

        /* @var $rule Mage_SalesRule_Model_Rule */
        $rule = $observer->getEvent()->getRule();


        /*if ($rule->getSimpleAction() != C4B_Freeproduct_Model_Consts::ADD_GIFT_ACTION
            || $item->getIsFreeProduct()
            || $rule->getIsApplied())
        {
            return;
        }*/

        if ($rule->getSimpleAction() != C4B_Freeproduct_Model_Consts::ADD_GIFT_ACTION
            || $item->getIsFreeProduct())
        {
            return;
        }

          //$item->geQty();

        try {
            //$qty = (int)$rule->getDiscountAmount();
            $qty = $item->geQty();
            //$skus = static::_getSkuList($rule->getGiftSku());
            $skus = static::_getSkuList( /*< Get your sku here for item having Id $item->getId() > */);

            foreach ($skus as $sku) {
                /** @var Mage_Sales_Model_Quote_Item $freeItem */
                $freeItem = static::_getFreeQuoteItem($rule->getId(), $sku, $item->getStoreId(), $qty);
                $quote->addItem($freeItem);
                $freeItem->setApplyingRule($rule);
            }

            //$rule->setData('is_applied', true);
        } catch (RuntimeException $e) {
            Mage::logException($e);
        }
    }

Create one cartrule from admin and select add gift. Add any dummy SKU as we are not using it anywhere.