Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

If the user adds the gift item manually and you want to "make it free", you can do this directly with a cart rule, no code needed:

In the "conditions" tab you select the products that trigger the rule, such as: enter image description here Trigger when I have at least 1 product with sku = dummy_8

Now in the "actions" tab: enter image description here Apply 100 "percent of product price discount" only to the product with the SKU you choose, in my case dummy_1. You should also set the maximum amount of products applied, so the customer gets just ONE item for free, doesn't matter how many items they add to the cart.

That should work! You can promote the action using banners and etc, it's quite faster without the need to code.

I think I got the solution. Thanks @Gopal Patel.

I need to listen to event "salesrule_validator_process"

In your Custom Module's etc/config.xml.

<global>
   .
        .
        .
   <events>
      <salesrule_validator_process>
         <observers>
            <{{ custom_module }}>
               <type>model</type>
               <class>{{custom_module}}/observer</class>
               <method>giftItemDiscount</method>
            </{{ custom_module }}>
         </observers>
      </salesrule_validator_process>
      .
         .
         .
   </events>
   .
        .
        .
</global>

Next add the function giftItemDiscount () in your module's observer.

public function giftItemDiscount(Varien_Event_Observer $observer) {
    $quote = $observer->getEvent()->getQuote();
    $item = $observer->getEvent()->getItem();
    $rule = $observer->getEvent()->getRule();
    /*$qty  = $item->getqty();*/
    $price = $item->getPrice() * $item->getqty();
    if ($item->getIsFreeGiftItem()) {
        $item->setDiscountPercent(100);
        $item->setDiscountAmount($price);
        $item->setBaseDiscountAmount($price);
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top