How to Restrict Magento 2 Checkout Based on Various Conditions
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
1 2 3 4 5 6 7 |
<?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
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 |
<?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!
Please feel free to post any doubts in the Comments section below!
Do rate the post with 5 stars.
Thank you.
Related Posts:
- How to Restrict Magento 2 Checkout Based on Various Conditions
- How to Disable Guest Checkout in Magento 2
- How to Restrict Order Using Plugin in Magento 2
Restrict checkout based minimum order amount for each customer groups with our extension.
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.
3 Comments
Thanks, this really helped…
Thank you for your appreciation. I’m glad the post was helpful to you 🙂
Thanks for covering this tutorial. Well, I would like to add one more step i,e Running CLI Commands:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:flush