Let’s initiate a discussion!!
If you want to restrict customer to checkout based on your criteria (condition), then this article is your answer.
For example if you want to restrict customer when cart total less then $300, this method is worth to give a try.
Here we are using Magemonkeys as Vendor name and CheckoutRestriction as the name of a module. You can change this according to your Vendor and Module name.
Step 1: Create Registration
Create registration.php file in the app/code/Magemonkeys/CheckoutRestriction folder with the following code.
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_CheckoutRestriction', __DIR__ ); |
Step 2: Create a module
Create a module.xml file in the app/code/Magemonkeys/CheckoutRestriction/etc folder with the following code.
1 2 3 4 5 6 7 8 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magemonkeys_CheckoutRestriction" setup_version="1.0.0"> <sequence> <module name="Magento_Checkout"/> </sequence> </module> </config> |
Step 3: Create a di.xml for front-end
Create a di.xml file in the app/code/Magemonkeys/CheckoutRestriction/etc/frontend folder with the following code.
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"> // plugin restrict checkout <type name="Magento\Checkout\Controller\Index\Index"> <plugin name="restrictcheckout" type="Magemonkeys\CheckoutRestriction\Plugin\Checkout\Controller\Restrict"/> </type> </config> |
Step 4: Create a Plugin file
Create a Restrict.php file in the app/code/Magemonkeys/CheckoutRestriction/Plugin/Checkout/Controller with the following code.
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 |
<?php namespace Magemonkeys\CheckoutRestriction\Plugin\Checkout\Controller; use Magento\Framework\Controller\Result\RedirectFactory; use Magento\Framework\Message\ManagerInterface; use Magento\Framework\UrlFactory; use Magento\Checkout\Controller\Index\Index; use Magento\Checkout\Model\Cart; class Restrict { private $urlModel; private $resultRedirectFactory; private $messageManager; public function __construct( UrlFactory $urlFactory, RedirectFactory $redirectFactory, ManagerInterface $messageManager, Cart $cart ) { $this->urlModel = $urlFactory; $this->resultRedirectFactory = $redirectFactory; $this->messageManager = $messageManager; $this->cart = $cart; } public function aroundExecute( Index $subject, \Closure $proceed ) { $this->urlModel = $this->urlModel->create(); //add your logic here $subTotal = $this->cart->getQuote()->getSubtotal(); if(!empty($subTotal) && $subTotal < 300){ $this->messageManager->addErrorMessage(__("Unable to checkout as one or more products have not met their minimum purchase theshold.")); $defaultUrl = $this->urlModel->getUrl('checkout/cart/', ['_secure' => true]); $resultRedirect = $this->resultRedirectFactory->create(); return $resultRedirect->setUrl($defaultUrl); } return $proceed(); } } |
Here is screenshot which shows error message when some one trying to click on checkout and subtotal less then $300
You can replace 300$ with your favorable amount and use accordingly.
[crayon-64216a567e1ca445388029/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.