0

I want to Limit Checkout Order or Add to Cart Limit to allow only 6 types of products/items. These 6 items can have variable Qty value. Eg: One Checkout/AddCart Maximum 6 Items: Item-1 25Qty, Item-2 125Qty, Item-3 Sample, Item-4 50Qty, Item-5 100Qty, Item-6 Sample,

So for I have created an extension using Event Observer but it is not working.

code of Vendor/Module/etc/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="controller_action_predispatch_checkout_cart_add">
        <observer name="limit_items_in_cart_checkout_before" instance="Vendor\Module\Observer\Checkout\Cart\Add" />
    </event>
</config>

code of Vendor/Module/Observer/Checkout/Cart/Add.php

<?php

namespace Vendor\Module\Observer\Checkout\Cart;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Response\RedirectInterface as ResponseRedirect;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface as MessageManager;

class Add implements ObserverInterface
{
    /** @var Session $session */
    private $session;

    /** @var ActionFlag $flag */
    private $flag;

    /** @var ResponseRedirect $redirect */
    private $redirect;

    /** @var MessageManager $messageManager */
    private $messageManager;

    /**
     * Add constructor.
     *
     * @param Session          $session
     * @param ActionFlag       $flag
     * @param ResponseRedirect $redirect
     * @param MessageManager   $messageManager
     */
    public function __construct(
        Session $session,
        ActionFlag $flag,
        ResponseRedirect $redirect,
        MessageManager $messageManager
    ) {
        $this->session         = $session;
        $this->flag            = $flag;
        $this->redirect        = $redirect;
        $this->messageManager  = $messageManager;
    }

    /**
     * @param Observer $observer
     *
     * @return $this|void
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function execute(Observer $observer)
    {
        $quote      = $this->session->getQuote();
        $itemsCount = $quote->getItemsCount();

        if ($itemsCount > 6) {
            $request = $observer->getRequest();
            $request->setParams([
                'product'    => false,
                'return_url' => $this->redirect->getRefererUrl(),
                'backUrl'    => $this->redirect->getRefererUrl()
            ]);

            $this->messageManager->addErrorMessage(
                __('Only 6 types of products per purchase allowed. Please complete this order first and place another order afterwards.')
            );

            $action = $observer->getEvent()->getControllerAction();
            $this->flag->set('', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH, true);
            $action->getResponse()->setRedirect('/checkout/cart');

            return $this;
        }
    }
}

1 Answer 1

0

Just update the code for your Observer Vendor/Module/Observer/Checkout/Cart/Add.php

<?php

namespace Vendor\Module\Observer\Checkout\Cart;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Response\RedirectInterface as ResponseRedirect;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface as MessageManager;

class Add implements ObserverInterface
{
    /** @var Session $session */
    private $session;

    /** @var ActionFlag $flag */
    private $flag;

    /** @var ResponseRedirect $redirect */
    private $redirect;

    /** @var MessageManager $messageManager */
    private $messageManager;

    /**
     * Add constructor.
     *
     * @param Session          $session
     * @param ActionFlag       $flag
     * @param ResponseRedirect $redirect
     * @param MessageManager   $messageManager
     */
    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Checkout\Model\Cart $cart,
        ResponseRedirect $redirect,
        MessageManager $messageManager
    ) {
        $this->_request        = $request;
        $this->_cart           = $cart;
        $this->redirect        = $redirect;
        $this->messageManager  = $messageManager;
    }

    /**
     * @param Observer $observer
     *
     * @return $this|void
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function execute(Observer $observer)
    {
        $controller = $observer->getControllerAction();
        $postValues = $this->_request->getPostValue();
        $cartQuote = $this->_cart->getQuote()->getData();
        $cartItemsCount = $this->_cart->getQuote()->getItemsCount();
        $cartItemsAll = $this->_cart->getQuote()->getAllItems();
 
        if($cartItemsCount > 5)
        {
            $observer->getRequest()->setParam('product', false);
            $observer->getRequest()->setParam('return_url', $this->redirect->getRefererUrl());
            $observer->getRequest()->setParam('backUrl', $this->redirect->getRefererUrl());
            $this->messageManager->addErrorMessage(
                __('Only 6 types of products per purchase allowed. Please complete this order first and place another order afterwards.')
            );
        }
    }
}

Hope this helps!

3
  • It is not working. @Sukumar
    – ishaq
    Commented Jan 8, 2021 at 14:24
  • It's working fine for me, only issue is message not displaying. Commented Jan 8, 2021 at 14:27
  • I am using Magneto 2.3.6 and updated Add.php with your updated code. and Added more than 6 types of products with variable quantity but not working, and no error message appearing.
    – ishaq
    Commented Jan 8, 2021 at 14:44

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.