Restrict place order Using Plugin/Interceptor in Magento 2
In some cases like placing an order, we can restrict customers to place the order. The common cases may be the low cart amount, geographical location issue, or any other issue.
We can easily achieve this functionality using the Magento 2 plugin also known as interceptors without touching the core files.
You can read about the interceptors through this link :
https://developer.adobe.com/commerce/php/development/components/plugins/
Also, you can follow our article on plugins –
https://webkul.com/blog/magento2-use-plugins/

Here are the steps to achieve this functionality –
We can validate the Magento 2 product before order placement using plugins. We are using the before method here.
We have created a separate module to achieve this which is a preferred and most secure way.
Here are the steps :
1) Firstly, Create a registration.php file inside the app/code/Vendor/Module folder and paste this code to register the module into your store :
<?php /** * Webkul Software. * * @category Webkul * @package Vendor_Module * @author Webkul <[email protected]> * @copyright Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html ASL Licence * @link https://store.webkul.com/license.html */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, "Vendor_Module", __DIR__ );
2) Secondly, to you have to create module.xml file inside app/code/Vendor/Module/etc folder –
<?xml version="1.0"?> <!-- /** * Webkul Software. * * @category Webkul * @package Vendor_Module * @author Webkul * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Module" /> </config>
The above two files are necessary to create and register our module in magento database.
3- After registering the module, create di.xml at path app\code\Vendor\Module\etc folder, which is dependency injection file and inject the plugin/interceptors dependency –
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <!-- For Logged in Customer --> <type name="Magento\Checkout\Api\PaymentInformationManagementInterface"> <plugin name="validate_order_with_plugin" type="Vendor\Module\Plugin\PlaceOrderValidationCheck"/> </type> <!-- Guest Customers --> <type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface"> <plugin name="restrict_guest_place_order" type="Vendor\Module\Plugin\GuestPlaceOrderValidationCheck"/> </type> </config>
4 – After this create PlaceOrderValidationCheck.php file at location for logged in magento customer: app\code\Vendor\Module\Plugin\PlaceOrderValidationCheck.php
<?php namespace Vendor\Module\Plugin; use Magento\Checkout\Api\PaymentInformationManagementInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Api\Data\PaymentInterface; use Magento\Quote\Api\Data\AddressInterface; /** * Class PlaceOrderValidationCheck */ class PlaceOrderValidationCheck { /** * Validate order submitting * * @param PaymentInformationManagementInterface $subject * @param int $cartId * @param PaymentInterface $paymentMethod * @param AddressInterface|null $billingAddress * @return void * @throws LocalizedException */ public function beforeSavePaymentInformationAndPlaceOrder( PaymentInformationManagementInterface $subject, $cartId, PaymentInterface $paymentMethod, AddressInterface $billingAddress = null ) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $grandTotal = $cart->getQuote()->getGrandTotal(); if ($grandTotal < 100) { throw new LocalizedException(__("You can't place the order.")); } } }
5- Now, you can create GuestPlaceOrderValidationCheck.php file at path which is used for guest customers place order: app\code\Vendor\Module\Plugin\GuestPlaceOrderValidationCheck.php
<?php namespace Vendor\Module\Plugin; use Magento\Checkout\Api\GuestPaymentInformationManagementInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Api\Data\AddressInterface; use Magento\Quote\Api\Data\PaymentInterface; /** * Class GuestPlaceOrderValidationCheck */ class GuestPlaceOrderValidationCheck { /** * Validate order submitting * * @param GuestPaymentInformationManagementInterface $subject * @param string $cartId * @param string $email * @param PaymentInterface $paymentMethod * @param AddressInterface|null $billingAddress * @return void * @throws LocalizedException */ public function beforeSavePaymentInformationAndPlaceOrder( GuestPaymentInformationManagementInterface $subject, $cartId, $email, PaymentInterface $paymentMethod, AddressInterface $billingAddress = null ) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $grandTotal = $cart->getQuote()->getGrandTotal(); if ($grandTotal < 100) { throw new LocalizedException(__("You can't place the order.")); } } }
6- Finally, run these commands in sequence:
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f php bin/magento cache:flush
That’s it! the functionality is achieved with after plugin easily.
Whenever a customer places a store order, the plugin will check the grand total quote class and validate the condition. If the condition matches the error place order functionality will not work and throw an error.
I hope this blog will help you in Magento 2 development also you may check our Magento 2 elastic search extension
Thanks
Current Product Version - 1.0.2
Supported Framework Version - 1.0
Be the first to comment.