{"id":1959,"date":"2021-11-19T16:10:49","date_gmt":"2021-11-19T16:10:49","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/restrict-customer-from-adding-product-to-cart-in-magento-2\/"},"modified":"2025-05-22T09:50:49","modified_gmt":"2025-05-22T04:20:49","slug":"restrict-customer-from-adding-product-to-cart-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/restrict-customer-from-adding-product-to-cart-in-magento-2\/","title":{"rendered":"How to Restrict Customer from Adding Product to Cart in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The post shows the solution to&nbsp;<em><strong>restrict customer from adding product to cart in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may use this solution when you are running an offer where conditions need to be checked for eligibility to avail discounts before the customers place an order. Till then, the admin can restrict customers to add products to the cart or update the cart.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if you are accepting orders based on conditions such as only a particular customer group can place an order, or customers from specific locations can place the order, in these cases, you may restrict them to add the product to the cart until the conditions are checked.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, whenever you want to confirm the order placement request from the backend and only after that allow customer to add another product or update the cart, use the below solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Restrict Customer from Adding Product to Cart in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add code in&nbsp;<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;type name=\"Magento\\Checkout\\Model\\Cart\">\n      &lt;plugin name=\"lock_customer_cart\" type=\"Vendor\\Module\\Plugin\\Lockcart\"\/>\n&lt;\/type><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create file&nbsp;<strong>Lockcart.php<\/strong>&nbsp;under&nbsp;<strong><strong><strong>app\\code\\vendor\\module\\Plugin\\<\/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;?php\nnamespace Vendor\\Module\\Plugin;\nuse Magento\\Framework\\Message\\ManagerInterface ;\nuse Magento\\Checkout\\Model\\Session;\nuse Magento\\Checkout\\Model\\Cart;\n\nclass Lockcart\n{\n\n    protected $_messageManager;\n    protected $quote;\n    \n    public function __construct(\n        Session $checkoutSession,\n        ManagerInterface $messageManager\n    ) {\n        $this->quote = $checkoutSession->getQuote();\n        $this->_messageManager = $messageManager;\n    }\n\n \n    public function beforeAddProduct(\n        Cart $subject,\n        $productInfo,\n        $requestInfo = null\n    ) {\n        $this->allowedMethod($subject);\n            \/\/ you can put custom condition and message here to restrict cart\n        return [$productInfo, $requestInfo];\n    }\n\n  \n    public function beforeUpdateItems(Cart $subject, $data)\n    {\n        $this->allowedMethod($subject);\n            \/\/ you can put custom condition and message here to restrict cart\n        return [$data];\n    }\n\n   \n    public function beforeUpdateItem(\n        Cart $subject,\n        $requestInfo = null,\n        $updatingParams = null\n    ) {\n        $this->allowedMethod($subject);\n            \/\/ you can put custom condition and message here to restrict cart\n        return [$requestInfo, $updatingParams];\n    }\n\n   \n    public function beforeRemoveItem(Cart $subject, $itemId)\n    {\n        $this->allowedMethod($subject);\n            \/\/ you can put custom condition and message here to restrict cart\n        return [$itemId];\n    }\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please 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>The post shows the solution to&nbsp;restrict customer from adding product to cart in Magento 2. You may use this solution when you are running an&#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-1959","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1959","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=1959"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1959\/revisions"}],"predecessor-version":[{"id":14183,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1959\/revisions\/14183"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}