Want to have complete control over customers’ checkout? Most often, Magento 2 store owners require to restrict customers’ purchase at the checkout by adding the conditions that are required to be fulfilled by them.
It is quite common for the store owners to restrict Magento 2 checkout based on various conditions like:
- Set minimum order amount required for the specific customer groups
- Sign up and login conditions for particular products. Eg: Sample Products
- Limit cart quantity for specific conditions. Eg: limit cart quantity based on category or customer
- Location Criteria
If you too want to try one of these conditions on the checkout of your Magento 2 store to increase the average order amount, encourage customers to upgrade their membership, ease the delivery process, etc. you may try the below method to restrict Magento 2 checkout based on various conditions.
For example, you want to restrict a customer to checkout with a sample product that can be bought only once. On his/her second attempt, restrict the checkout with the below code! Sometimes, you may also want to disable checkout in Magento 2, when your store is in the maintenance mode. Also when your Magento 2 checkout is slow use advanced techniques to speed up your store checkout.
You may also set the conditions to limit the cart quantity based on the product quantity ordered from each category for every customer group.
Method to Restrict Magento 2 Checkout Based on Various Conditions:
Implement the below code at app\code\Vendor\Extension\etc\frontend\di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Checkout\Controller\Index\Index"> <plugin name="restrictcheckout" type="Vendor\Extension\Plugin\Checkout\Controller\Restrict"/> </type> </config>
Implement the below code at app\code\Vendor\Extension\Plugin\Checkout\Controller\Restrict.php
<?php namespace Vendor\Extension\Plugin\Checkout\Controller; use Magento\Framework\Controller\Result\RedirectFactory; use Magento\Framework\Message\ManagerInterface; use Magento\Framework\UrlFactory; use Magento\Checkout\Controller\Index\Index; class Restrict { private $urlModel; private $resultRedirectFactory; private $messageManager; public function __construct( UrlFactory $urlFactory, RedirectFactory $redirectFactory, ManagerInterface $messageManager ) { $this->urlModel = $urlFactory; $this->resultRedirectFactory = $redirectFactory; $this->messageManager = $messageManager; } public function aroundExecute( Index $subject, \Closure $proceed ) { $this->urlModel = $this->urlModel->create(); //your condition // code for redirect to cart page with error message //$this->messageManager->addErrorMessage(__('Error Message.')); //$defaultUrl = $this->urlModel->getUrl('checkout/cart/', ['_secure' => true]); //$resultRedirect = $this->resultRedirectFactory->create(); //return $resultRedirect->setUrl($defaultUrl); return $proceed(); } }
With the above methods, you can tweak and twist the checkout conditions in Magento 2 store!
Thank you.