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
<!--?xml version="1.0"?--> <!-- For Login Customer --> <!-- For Guest Customer -->
Step 2: Create PaymentInfoManagement.php at path app\code\Vendor\Module\Plugin\PaymentInfoManagement.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 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
<?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.