Hello Everyone,
In this blog, we will learn about how to Restrict Order Place Using Plugin in Magento 2.
It is useful when you want to restrict your customer to fulfill some conditions before placing the order.
So that we create a new plugin then will check the condition before placing the order place.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Restrict Order Place Using Plugin in Magento 2.
STEPS FOR RESTRICT TO PLACE ORDER USING PLUGIN IN MAGENTO 2
Step 1: Create di.xml file
app/code/Vendor/Extension/etc/di.xml
<?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="magecurious_order_restrict_plugin" type="Vendor\Extension\Plugin\OrderValidationCheck"/>
</type>
<!-- Guest Customers -->
<type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
<plugin name="magecurious_guest_order_restrict_plugin" type="Vendor\Extension\Plugin\GuestOrderValidationCheck"/>
</type>
</config>
Step 2: Create OrderValidationCheck.php file
app/code/Vendor/Extension/Plugin/OrderValidationCheck.php
<?php
namespace Vendor\Extension\Plugin;
use Magento\Checkout\Api\PaymentInformationManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\CartRepositoryInterface;
/**
* Class PlaceOrderValidationCheck
*/
class OrderValidationCheck
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* OrderValidationCheck constructor.
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
CartRepositoryInterface $cartRepository
) {
$this->cartRepository = $cartRepository;
}
/**
* 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
) {
$cart = $this->cartRepository->get($cartId);
$grandTotal = $cart->getGrandTotal();
if ($grandTotal < 100) {
throw new LocalizedException(__("Order Amount is Less then 100."));
}
}
}
Step 3: Create GuestOrderValidationCheck.php file
app/code/Vendor/Extension/Plugin/GuestOrderValidationCheck.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;
use Magento\Quote\Api\CartRepositoryInterface;
/**
* Class GuestOrderValidationCheck
*/
class GuestPlaceOrderValidationCheck
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* OrderValidationCheck constructor.
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
CartRepositoryInterface $cartRepository
) {
$this->cartRepository = $cartRepository;
}
/**
* 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
)
{
$cart = $this->cartRepository->get($cartId);
$grandTotal = $cart->getGrandTotal();
if ($grandTotal < 100) {
throw new LocalizedException(__("Order Amount is Less then 100."));
}
}
}
Step 4: Finally run the below commands
$ php bin/magento setup:upgrade
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 5: Output:
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Create a Plugin to Restrict Order Place in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
