Restrict Place Order Using Plugin

Restrict place order using plugin/interceptor in magento 2

In certain cases, such as ordering, we may restrict customers from placing orders. Common cases could be low cart amount, geographic location issue, or some other issue.

We can easily achieve this functionality by using magento 2 plugin known as interceptor without touching the key files.

You can read about interceptor 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 magento 2 product before order placement by using plugins. We are using the earlier method here.

We have created a separate module which is a preferred and safest way to achieve this.

Here are the steps:

1) First, create a registration.php file inside app/code/vendor/modules folder and paste this code to register the module in 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, you need to create module.xml file inside app/code/vendor/modules/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>

Above two files are required to create and register our module in magento database.

3- After registering the module, create di.xml in the path app\code\vendor\modules\etc folder, which is the dependency injection file and inject the plugin/interceptor 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 – Then create PlaceOrderValidationCheck.php file at location to log in magento client: 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 on the path that is used for ordering for guest clients: 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

this much only! The functionality is easily achieved with plugins.

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 then the order functionality will not work and will throw an error.

Current Product Version – 1.0.2

Supported Framework Version – 1.0

Leave a Comment