How To Restrict Order Using Plugin in Magento 2
Our earlier post showed you how to restrict ‘Add to Cart’ using a plugin in Magento 2. Today, I will show you how to restrict order using plugin in Magento 2.
It is helpful when you want the customers to fulfill certain conditions before placing the order. Suppose you want to restrict customers from placing orders less than a certain amount in your store or want to restrict customers from certain geographical locations. In any of these cases, you may require to check the order through conditions before the placement.
I’ve come up with a solution to restrict the customer from placing the order through conditions.
So, let’s get started!
Method to Restrict Order Using Plugin in Magento 2
To restrict the order in Magento 2 through conditions, we will be using the plugin method. In this method, we need to create a new plugin that will check the conditions before the order is placed in Magento 2. You can follow the below-mentioned steps to validate the order before being placed in Magento 2.
Step 1: Create di.xml at path app\code\Vendor\Module\etc\di.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <!-- For Login Customer --> <type name="Magento\Checkout\Api\PaymentInformationManagementInterface"> <plugin name="restrict_place_order" type="Vendor\Module\Plugin\PaymentInfoManagement"/> </type> <!-- For Guest Customer --> <type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface"> <plugin name="restrict_guest_place_order" type="Vendor\Module\Plugin\GuestPaymentInfoManagement"/> </type> </config> |
Step 2: Create PaymentInfoManagement.php at path app\code\Vendor\Module\Plugin\PaymentInfoManagement.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 |
<?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 PaymentInfoManagement */ class PaymentInfoManagement { /** * 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 ) { if (condition true) { throw new LocalizedException(__("The order can't be placed.")); } } } |
Step 3: Create GuestPaymentInfoManagement.php at path app\code\Vendor\Module\Plugin\GuestPaymentInfoManagement.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 |
<?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 GuestPaymentInfoManagement */ class GuestPaymentInfoManagement { /** * 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 ) { if (condition true) { throw new LocalizedException(__("The order can't be placed.")); } } } |
And that’s it!
Hope it helps you!
If you have any doubts or queries, please feel free to comment. Iād be happy to help you. š
Also, Iād be grateful if you could share the solution with the Magento community via social media. š
Thank you. š
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
Shopify vs WordPress SEO: Which One is Best for You? [2024]
15 Best Shopify Clothing Store Examples to Get Some Fresh Inspiration
Next