If you want to incentivize your customers to put only 1 product in the shopping cart, then this the right place you have arrived. The Magento 2 shop owner aspires to restrict one product per order, sticking to some of the following reasons:
When the production rates do not exceed the demand in the market, the owner needs to undergo this strategy. Such conditions do arise in real time scenario. It might be too tedious for the seller to dispatch a hefty number of items, at a time for a single order due to expensive production cost.
Among the best marketing practices, lies the secret of reaching out to more consumers, instead of selling items in large quantities to a smaller group. In the scenario when the warehouse is left with scanty supply, committee agrees to sell 1 product per order to fetch more significant customers. Considering the business prospective, applying the right tactics is the need of the hour.
To implement this technically, there are fix lines of code developed by our team of experts in Magento 2, since the software Magento 2 doesn’t support this option.
Now, enough of talking , let’s unleash those magical lines of code which complies the condition of only 1 product per order in Magento 2.
Steps to Force Only One Product Per Order in Magento 2:
Step 1: Create event.xml file at etc directory
<?xml version=”1.0″?> <config xmlns:xsi=”https://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=”jhk_restrinc_addtocart” instance=”Vendor\Extension\Observer\Cartadd” /> </event> </config>
Step 2: Create Cartadd.php file at Observer directory
<?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\App\ObjectManager; class Cartadd implements ObserverInterface { protected $_urlManager; protected $_checkoutSession; protected $_cart; protected $_messageManager; protected $_redirect; protected $_request; protected $_response; protected $_responseFactory; protected $_resultFactory; protected $_scopeConfig; protected $_product; public function __construct(\Magento\Framework\UrlInterface $urlManager, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\Response\RedirectInterface $redirect, \Magento\Checkout\Model\Cart $cart, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\App\ResponseInterface $response, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product $product, \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\Controller\ResultFactory $resultFactory ) { $this->_urlManager = $urlManager; $this->_checkoutSession = $checkoutSession; $this->_redirect = $redirect; $this->_cart = $cart; $this->_messageManager = $messageManager; $this->_request = $request; $this->_response = $response; $this->_responseFactory = $responseFactory; $this->_resultFactory = $resultFactory; $this->_scopeConfig = $scopeConfig; $this->_product = $product; } public function execute(\Magento\Framework\Event\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 > 0) { $observer->getRequest()->setParam(‘product’, false); $observer->getRequest()->setParam(‘return_url’, $this->_redirect->getRefererUrl()); $observer->getRequest()->setParam(‘backUrl’, $this->_redirect->getRefererUrl()); $this->_messageManager->addError(__(‘Only 1 product Allowed to Purchase at a time.’)); } } }
I hope you got a satisfactory answer for all your queries. If any further doubts persist, then do mention in the comments section below. Share this article if you enjoyed reading it!