{"id":876,"date":"2020-03-13T12:23:36","date_gmt":"2020-03-13T12:23:36","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/13\/restrict-add-to-cart-using-plugin-in-magento-2\/"},"modified":"2025-05-22T14:46:36","modified_gmt":"2025-05-22T09:16:36","slug":"restrict-add-to-cart-using-plugin-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/restrict-add-to-cart-using-plugin-in-magento-2\/","title":{"rendered":"How To Restrict &#8220;Add To Cart&#8221; Using Plugin In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Earlier, I posted a solution to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-conditions-to-restrict-adding-products-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">set conditions to restrict adding products in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This time, solving a similar issue, however, with the plugin is implemented in an easier way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the below code to&nbsp;<em><strong>restrict \u201cAdd to Cart\u201d using plugin in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you can restrict a customer to make a purchase of products from different categories. Or, restrict purchasing a demo product for more than once!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Such conditions can be set to restrict adding products to cart in Magento 2 using the below code:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Restrict \u201cAdd To Cart\u201d Using Plugin In Magento 2:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create&nbsp;<em><strong>di.xml<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/code\/[Vendor]\/[Module]\/etc<\/strong>&nbsp;and add the following code:<\/li>\n<\/ul>\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\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Checkout\\Model\\Cart\">\n        &lt;plugin name=\"interceptAddingProductToCart\"\n                type=\"[Vendor]\\[Module]\\Plugin\\Model\\Checkout\\Cart\\Plugin\"\n                sortOrder=\"10\"\n                disabled=\"false\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create&nbsp;<em><strong>Plugin.php<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/code\/[Vendor]\/[Module]\/Plugin\/Model\/Checkout\/Cart<\/strong>&nbsp;and add the following code:<\/li>\n<\/ul>\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\n\nnamespace [Vendor]\\[Module]\\Plugin\\Model\\Checkout\\Cart;\n\n\nuse Magento\\Framework\\Exception\\LocalizedException;\n\nclass Plugin\n{\n    \n    public function beforeAddProduct($subject, $productInfo, $requestInfo = null)\n    {\n\n        try {\n    \n            \/\/ Your custom code here.\n        } catch (\\Exception $e) {\n            throw new LocalizedException(__($e->getMessage()));\n        }\n\n        return [$productInfo, $requestInfo];\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\">Or, you could simply use the&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-call-for-price.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Call for Price<\/a>&nbsp;extension to restrict customers to add products to the cart and instead call you for inquiring about pricing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a similar way, you can also use the&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/restrict-order-using-plugin-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">plugin method to restrict orders in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post 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>Earlier, I posted a solution to&nbsp;set conditions to restrict adding products in Magento 2. This time, solving a similar issue, however, with the plugin is&#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-876","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/876","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=876"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/876\/revisions"}],"predecessor-version":[{"id":15033,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/876\/revisions\/15033"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}