2

I have to add some discount programmatically on the time of bundle product add to cart.

So far I have developed the catalog product plugin Magento\Catalog\Model\Product function afterGetFinalPrice this plugin is worked on the basis of their child product prices so I have used another bundle price plugin Magento\Bundle\Model\Product\Price function afterGetFinalPrice but it won't return desired output.

As per the Magento default flow, Magento is applying a discount on its child product but I like to apply the discount on bundle product total price.

0

You can create following files in your custom extension.

etc/frontend/events.xml

<?xml version="1.0" encoding="UTF-8"  ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_item_set_product">
        <observer name="bundle_addto_cart_before" instance="[Vendor]\[Module]\Observer\SetProduct" />
    </event>
</config>

File:

Observer/SetProduct.php

<?php

namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SetProduct implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $quoteItem = $observer->getEvent()->getQuoteItem();
        $product = $quoteItem->getProduct();
        if(!$quoteItem->getId() && $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
        {
            $originalPrice = $product->getFinalPrice();
            $customPrice = 22.5;
            $quoteItem->setPrice($customPrice);
            $quoteItem->setBasePrice($customPrice);
            $quoteItem->setCustomePrice(null);
            $quoteItem->setOriginalCustomPrice(null);
            $quoteItem->getProduct()->setIsSuperMode(false);
        }

        return $this;
    }
}

This should work.

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.