As a Magento 2 store owner, in the event of a limited number of stocks or while trying a new selling strategy, you may want to limit the number of items to be purchased per order to market it among the more massive audience.
Magento 2 doesn’t provide you this option; this functionality has to be configured programmatically.
If you want to implement Only One Product Per Order in Magento 2 functionality for the customer, then follow the below steps to create a custom module.
Step 1: Create event.xml
<?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="magemonkeys_restrictitem_tocart" instance="MagemonkeysRestrictitemtocartObserverProductadd" /> </event> </config>
Step 2: Create a Productadd.php Observer in Observer Folder.
<?php
namespace MagemonkeysRestrictitemtocartObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestDataPersistorInterface;
use MagentoFrameworkAppObjectManager;
class Productadd 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 item is allowed to purchase or add.'));
}
}
}
After creating the above module, please run the upgrade, decompile and static:content:deploy command to implement this module.
Hope you find this guide useful and may it helps you to limit the product quantity per user. If you are facing problem during this process, then feel free to comment.

