Question

How to check if product is a bundle in the checkout_cart_add_product_complete event?

I want to redirect the page after adding to the cart when the product is bundled.

Was it helpful?

Solution

You can get it from this way

etc/frontend/event.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="checkout_cart_add_product_complete">
             <observer name="check_bundle_event" instance="Vendor\Magento\Observer\CheckBundledItem"/>
        </event>
</config>

2) Now call CheckBundledItem in path Vendor\Magento\Observer\

<?php
namespace Vendor\Magento\Observer;

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

class CheckBundledItem implements ObserverInterface {

    public function execute(Observer $observer) {
        /** @var \Magento\Catalog\Model\Product\Interceptor $product */
        $product = $observer->getProduct();
        if($product->getTypeId() == 'bundle'){
          //ADD YOUR CODE HERE
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top