1

say for eg in same category

Buy A and B then C product should be free or 2A then B should be free

0

Finally I did it by myself so these are the steps I followed to achieve it

  1. I am using magento version 2.1.6. first I've created a new cart price rule find it in below image

enter image description here

  1. Since I am using event "salesrule_validator_process" so in module etc/events.xml I wrote like this
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
     <event name="salesrule_validator_process">
        <observer name="custom_cart_rules" instance="Almusbah\SendSms\Observer\CustomCartPriceRules" />
    </event>
</config>
  1. Finally Observer file "CustomCartPriceRules.php" does the magic

    <?php
    
    namespace Almusbah\SendSms\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\ObjectManager;
    
    class CustomCartPriceRules implements ObserverInterface {
        public function __construct() {
    
        }
    
        /**
         *
         * @param \Magento\Framework\Event\Observer $observer
         * @return void
         */
        public function execute(\Magento\Framework\Event\Observer $observer) {
    
            $result = $observer->getEvent()->getResult();
            $item = $observer->getEvent()->getItem();
            $rule = $observer->getEvent()->getRule();
            $address = $observer->getEvent()->getAddress();
            $qty = $item->getQty();
    
            if ($rule->getData('name') == 'Buy 2 get 1 free'):
    
                    // overriding magento rules here and updating Almusbah Offer
                    $result->setAmount(0)
                        ->setBaseAmount(0)
                        ->setOriginalAmount(0)
                        ->setBaseOriginalAmount(0);
    
                    $item->setDiscountPercent(0);
    
    
                $products = [];
                $z = 0;
                $totalQty = 0;
                $dA = 0;
                foreach ($address->getAllItems() as $q):
    
                    $validate = $rule->getActions()->validate($q);
    
                    if ($validate):
                        $products[$z]['sku'] = $q->getData('sku');
                        $products[$z]['qty'] = $q->getData('qty');
                        $totalQty += $q->getData('qty');
                        $products[$z]['totalQty'] = $totalQty;
                        $freeQty = (int) ($totalQty / 3);
                        $products[$z]['freeQty'] = $freeQty;
                        if ($products[$z]['freeQty'] > 0):
                            $products[$z]['applyDiscount'] = 1;
                            $dicountPercent = 100;
                            $dA = ($products[$z]['freeQty'] * $q->getData('calculation_price'));
                            $products[$z]['discountAmount'] = $dA;
                            $totalQty -= 3*$products[$z]['freeQty']; // appying on each 3 items
                        endif;
                        $z++;
                    endif;
                endforeach;
                //echo '<pre>';print_r($products);
                $chkCurrentSku = $item->getSku();
    
                foreach ($products as $p):
                    if(isset($p['applyDiscount']) && !empty($p['applyDiscount']) && $chkCurrentSku == $p['sku']):
                        $discountAmount = $p['discountAmount'];
                        $fullDiscount = 100;
                        $item->setDiscountPercent($fullDiscount);
                        $this->almusbahBuy2Get1Offer($discountAmount,$result);
                    endif;
                endforeach;
    
            endif;
    
    
        }
    
        public function almusbahBuy2Get1Offer($discountAmount, $result) {
    
                       $result->setAmount($discountAmount)
                            ->setBaseAmount($discountAmount)
                            ->setOriginalAmount($discountAmount)
                            ->setBaseOriginalAmount($discountAmount);
                }
    
        }
    

    ?>

  2. That's it don't forget to update the module using

    php bin/magento setup:upgrade

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.