{"id":2135,"date":"2022-10-05T07:30:53","date_gmt":"2022-10-05T07:30:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/restrict-order-using-plugin-in-magento-2\/"},"modified":"2025-03-17T08:45:25","modified_gmt":"2025-03-17T08:45:25","slug":"restrict-order-using-plugin-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/restrict-order-using-plugin-in-magento-2\/","title":{"rendered":"How To Restrict Order Using Plugin in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Our earlier post showed you how to <a title=\"How to Restrict 'Add to Cart' Using Plugin in Magento 2\" href=\"https:\/\/meetanshi.com\/blog\/restrict-add-to-cart-using-plugin-in-magento-2\/\" target=\"_blank\" rel=\"noopener\">restrict &#8216;Add to Cart&#8217; using a plugin in Magento 2<\/a>. Today, I will show you how to restrict order using plugin in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve come up with a solution to restrict the customer from placing the order through conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, let&#8217;s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method to Restrict Order Using Plugin in Magento 2<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:<\/strong> Create <strong>di.xml<\/strong> at path <strong><strong>app\\code\\Vendor\\Module\\etc\\di.xml<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!--?xml version=\"1.0\"?-->\n\n&lt;!-- For Login Customer -->\n    \n        \n    \n&lt;!-- For Guest Customer --><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong> Create <strong>PaymentInfoManagement.php<\/strong> at path <strong><strong>app\\code\\Vendor\\Module\\Plugin\\PaymentInfoManagement.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&amp;lt;?php\n\nnamespace Vendor\\Module\\Plugin;\n\nuse Magento\\Checkout\\Api\\PaymentInformationManagementInterface;\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Quote\\Api\\Data\\PaymentInterface;\nuse Magento\\Quote\\Api\\Data\\AddressInterface;\n\n\/**\n * Class PaymentInfoManagement\n *\/\nclass PaymentInfoManagement\n{\n    \/**\n     * Validate order submitting\n     *\n     * @param PaymentInformationManagementInterface $subject\n     * @param int $cartId\n     * @param PaymentInterface $paymentMethod\n     * @param AddressInterface|null $billingAddress\n     * @return void\n     * @throws LocalizedException\n     *\/\n    public function beforeSavePaymentInformationAndPlaceOrder(\n        PaymentInformationManagementInterface $subject,\n        $cartId,\n        PaymentInterface $paymentMethod,\n        AddressInterface $billingAddress = null\n    ) {\n        if (condition true) {\n        throw new LocalizedException(__(\"The order can't be placed.\"));\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:<\/strong> Create <strong>GuestPaymentInfoManagement.php<\/strong> at <strong>path <strong>app\\code\\Vendor\\Module\\Plugin\\GuestPaymentInfoManagement.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&amp;lt;?php\n\nnamespace Vendor\\Module\\Plugin;\n\nuse Magento\\Checkout\\Api\\GuestPaymentInformationManagementInterface;\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Quote\\Api\\Data\\AddressInterface;\nuse Magento\\Quote\\Api\\Data\\PaymentInterface;\n\n\/**\n * Class GuestPaymentInfoManagement\n *\/\nclass GuestPaymentInfoManagement\n{\n    \/**\n     * Validate order submitting\n     *\n     * @param GuestPaymentInformationManagementInterface $subject\n     * @param string $cartId\n     * @param string $email\n     * @param PaymentInterface $paymentMethod\n     * @param AddressInterface|null $billingAddress\n     * @return void\n     * @throws LocalizedException\n     *\/\n    public function beforeSavePaymentInformationAndPlaceOrder(\n        GuestPaymentInformationManagementInterface $subject,\n        $cartId,\n        $email,\n        PaymentInterface $paymentMethod,\n        AddressInterface $billingAddress = null\n    )\n    {\n        if (condition true) {\n             throw new LocalizedException(__(\"The order can't be placed.\"));\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And that&#8217;s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hope it helps you!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any doubts or queries, please feel free to comment. I\u2019d be happy to help you.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, I\u2019d be grateful if you could share the solution with the Magento community via social media.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our earlier post showed you how to restrict &#8216;Add to Cart&#8217; using a plugin in Magento 2. Today, I will show you how to restrict&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2135","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2135"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2135\/revisions"}],"predecessor-version":[{"id":9481,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2135\/revisions\/9481"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}