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 |
<?php namespace VendorExtensionObserver; use MagentoFrameworkEventObserverInterface; use MagentoFrameworkAppRequestDataPersistorInterface; use MagentoFrameworkAppObjectManager; 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(MagentoFrameworkUrlInterface $urlManager, MagentoCheckoutModelSession $checkoutSession, MagentoFrameworkAppResponseRedirectInterface $redirect, MagentoCheckoutModelCart $cart, MagentoFrameworkMessageManagerInterface $messageManager, MagentoFrameworkAppRequestInterface $request, MagentoFrameworkAppResponseInterface $response, MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig, MagentoCatalogModelProduct $product, MagentoFrameworkAppResponseFactory $responseFactory, MagentoFrameworkControllerResultFactory $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(MagentoFrameworkEventObserver $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 🙂