{"id":399,"date":"2019-05-01T12:01:05","date_gmt":"2019-05-01T12:01:05","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/05\/01\/restrict-magento-2-checkout-based-on-various-conditions\/"},"modified":"2025-05-22T16:55:24","modified_gmt":"2025-05-22T11:25:24","slug":"restrict-magento-2-checkout-based-on-various-conditions","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/restrict-magento-2-checkout-based-on-various-conditions\/","title":{"rendered":"How to Restrict Magento 2 Checkout Based on Various Conditions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Want to have complete control over customers&#8217; checkout? Most often, Magento 2 store owners require to restrict customers&#8217; purchase at the checkout by adding the conditions that are required to be fulfilled by them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><i><\/i>It is quite common for the store owners to <i><strong>restrict Magento 2 checkout based on various conditions<\/strong><\/i> like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a title=\"How to Set Minimum Order Amount in Magento 2\" href=\"https:\/\/meetanshi.com\/blog\/set-minimum-order-amount-in-magento-2\/\">Set minimum order amount<\/a> required for the specific customer groups<\/li>\n\n\n\n<li>Sign up and login conditions for particular products. Eg: Sample Products<\/li>\n\n\n\n<li>Limit cart quantity for specific conditions. Eg: limit cart quantity based on category or customer<\/li>\n\n\n\n<li>Location Criteria<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If you too want to try one of these conditions on the checkout of your Magento 2 store to increase the average order amount, encourage customers to upgrade their membership, ease the delivery process, etc. you may try the below method to <i><strong>restrict Magento 2 checkout based on various conditions<\/strong><\/i>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you want to restrict a customer to checkout with a sample product that can be bought only once. On his\/her second attempt, restrict the checkout with the below code! Sometimes, you may also want to <a title=\"How to Disable Checkout in Magento 2\" href=\"https:\/\/meetanshi.com\/blog\/disable-checkout-in-magento-2\/\" target=\"_blank\" rel=\"noopener\">disable checkout in Magento 2<\/a>, when your store is in the maintenance mode. Also when your <a title=\"Magento 2 Checkout Slow: Advanced Techniques To Speed Up\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-checkout-slow\/\">Magento 2 checkout is slow<\/a> use advanced techniques to speed up your store checkout.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may also set the conditions to limit the cart quantity based on the product quantity ordered from each category for every customer group.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Restrict Magento 2 Checkout Based on Various Conditions:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Implement the below code at <strong><strong><strong>app\\code\\Vendor\\Extension\\etc\\frontend\\di.xml<\/strong><\/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&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Checkout\\Controller\\Index\\Index\">\n        &lt;plugin name=\"restrictcheckout\" type=\"Vendor\\Extension\\Plugin\\Checkout\\Controller\\Restrict\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Implement the below code at<strong> <strong>app\\code\\Vendor\\Extension\\Plugin\\Checkout\\Controller\\Restrict.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=\"\">&lt;?php\n \nnamespace Vendor\\Extension\\Plugin\\Checkout\\Controller;\n \nuse Magento\\Framework\\Controller\\Result\\RedirectFactory;\nuse Magento\\Framework\\Message\\ManagerInterface;\nuse Magento\\Framework\\UrlFactory;\nuse Magento\\Checkout\\Controller\\Index\\Index;\n \nclass Restrict\n{\n    private $urlModel;\n    private $resultRedirectFactory;\n    private $messageManager;\n \n    public function __construct(\n        UrlFactory $urlFactory,\n        RedirectFactory $redirectFactory,\n        ManagerInterface $messageManager\n    ) {\n    \n        $this->urlModel = $urlFactory;\n        $this->resultRedirectFactory = $redirectFactory;\n        $this->messageManager = $messageManager;\n    }\n \n    public function aroundExecute(\n        Index $subject,\n        \\Closure $proceed\n    ) {\n    \n        $this->urlModel = $this->urlModel->create();\n        \n \/\/your condition\n \n \/\/ code for redirect to cart page with error message\n        \/\/$this->messageManager->addErrorMessage(__('Error Message.'));\n        \/\/$defaultUrl = $this->urlModel->getUrl('checkout\/cart\/', ['_secure' => true]);\n        \/\/$resultRedirect = $this->resultRedirectFactory->create();\n        \/\/return $resultRedirect->setUrl($defaultUrl);\n          \n        return $proceed();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the above methods, you can tweak and twist the checkout conditions in Magento 2 store!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to have complete control over customers&#8217; checkout? Most often, Magento 2 store owners require to restrict customers&#8217; purchase at the checkout by adding the&#8230;<\/p>\n","protected":false},"author":5,"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-399","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/399","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":15448,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions\/15448"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}