How to Force Only One Product Per Order in Magento 2
A Magento 2 store owner may want to limit the number of items to be purchased per order due to limited stock or some selling strategy. This may even be the case when production conditions come to picture. It might not be feasible for the store owner to deliver a number of items at a time for a single order due to higher production cost.
Moreover, according to a marketing strategy, it is good to reach to more people rather than selling products in large quantity to a smaller crowd. When there is a limited stock, admin decides to sell one product per order to market it among more potential customers.
The condition to Force Only One Product Per Order in Magento 2 has to be configured programmatically as the default Magento 2 does not provide this option.
I’ll give the steps to implement the condition to Force Only One 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
1 2 3 4 5 6 7 8 9 10 11 |
<?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="controller_action_predispatch_checkout_cart_add"> <observer name="meetanshi_restrinc_addtocart" instance="Vendor\Extension\Observer\Cartadd" /> </event> </config> |
Step 2: Create Cartadd.php file at Observer directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?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.')); } } } |
That’s all you need to do to Force Only One Product Per Order in Magento 2!
Any doubts on the topic are welcome in the Comments section.
Rate the post with 5 stars if you liked it.
Happy Coding 🙂
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Where is the Observer directory in Magento 2.4.1?
Hello Sonu,
You have to implement the above code in your custom extension, if any.
Thanks