1

All the Free products need to remove from cart if customer apply coupon code. Actual product should remain same in cart. I do have specific category for free products.

Help me to figure it out how to achieve this functionality.

Thanks.

5
  • 1
    you need to used magento event observer to archive this. Feb 15, 2017 at 12:54
  • 1
    @DhirenVasoya - Can u please explain it little bit more to me? It would be great if u share me some code reference.. Thanks
    – Kausik Dey
    Feb 15, 2017 at 12:59
  • I post answer, please check. Feb 15, 2017 at 13:08
  • 1
    @DhirenVasoya Thanks. :) Let me try that code.
    – Kausik Dey
    Feb 15, 2017 at 13:10
  • sure, If answer help accept so it will help other in community as well. Feb 15, 2017 at 13:11

2 Answers 2

1

You need to create an observer on sales_quote_collect_totals_after

<events>
    <sales_quote_collect_totals_after>
      <observers>
        <[namespace]_[modulename]_sales_quote_collect_totals_after>
          <type>singleton</type>
          <class>[Module]_[Namespace]_Model_Observer</class>
          <method>salesQuoteCollectTotalsAfter</method>
        </[namespace]_[modulename]_sales_quote_collect_totals_after>
      </observers>
    </sales_quote_collect_totals_after>
</events>

In your observer.php use

class [Module]_[Namespace]_Model_Observer
{
   salesQuoteCollectTotalsAfter($observer)
   {
       $quote = $observer->getQuote();
       $cartHelper = Mage::helper('checkout/cart');
       $couponcode = $quote->getData('coupon_code');
       if ($couponcode == '[YOUR_COUPON_CODE]') {
            foreach($quote->getAllItems() as $item):
                $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item->getSku());
                $cartProductCatId = $_product->getCategoryIds();
                    if(is_array($cartProductCatId) && in_array('[YOUR_CATEGORY_ID]',$cartProductCatId)){
                        $itemId = $item->getItemId();
                        $cartHelper->getCart()->removeItem($itemId)->save();
                    }
            endforeach;
       }
   }
}
0

You can used magento event observer in your custom extension like.

  vendor\extension\etc\config.xml

<global>
  <events>
    <sales_quote_collect_totals_after>
      <observers>
        <vendor_extension_sales_quote_collect_totals_after>
          <type>singleton</type>
          <class>vendor_extension_Model_Observer</class>
          <method>salesQuoteCollectTotalsAfter</method>
        </vendor_extension_sales_quote_collect_totals_after>
      </observers>
    </sales_quote_collect_totals_after>
  </events>
</global>

Now create Observer file.

vendor\extension\Model\Observer.php

class vendor_extension_Model_Observer
{
   salesQuoteCollectTotalsAfter($observer)
   {
       $quote = $observer->getQuote();
       $couponcode = $quote->getData('coupon_code');
       if ($couponcode != '') {
          /**
           * Your custom code here to remove free item
           */
       }
       return $this;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.