{"id":143,"date":"2018-07-30T05:33:49","date_gmt":"2018-07-30T05:33:49","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/07\/30\/set-conditions-to-restrict-adding-products-magento-2\/"},"modified":"2025-05-22T17:25:04","modified_gmt":"2025-05-22T11:55:04","slug":"set-conditions-to-restrict-adding-products-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/set-conditions-to-restrict-adding-products-magento-2\/","title":{"rendered":"How to Set Conditions to Restrict Adding Products in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello Magento folks!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I am back with another technical solution on \u2018How to restrict \u201cAdd To Cart\u201d using event in Magento 2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Product management becomes tricky when you want to sell products under the conditions. Some of the examples are allowing to add only one product in the cart, adding only one product from each category, only adding one product type in the cart, no two products can be added with each other, and many other rules. To apply such rules, you have to&nbsp;<em><strong>Set Conditions to Restrict Adding Products in Magento 2&nbsp;<\/strong><\/em>and<em><strong>&nbsp;<\/strong><\/em>work on cart&nbsp;conditions and events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I\u2019ve come up with the custom code to set conditions to restrict adding Magento 2 products to the shopping cart. The only thing you have to do is define the condition as per your requirement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s begin with the solution to restrict \u2018add to cart\u2019 in Magento 2 using conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Set Conditions to Restrict Adding Products in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>event.xml<\/strong>&nbsp;in the&nbsp;<strong>etc<\/strong>&nbsp;folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" 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:Event\/etc\/events.xsd\">\n    &lt;event name=\"controller_action_predispatch_checkout_cart_add\">\n        &lt;observer name=\"restrict_sales_model_cart_add_before\" instance=\"Vendor\\Extension\\Observer\\Cartadd\"\/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>Cartadd.php<\/strong>&nbsp;file under the&nbsp;<strong>Observer<\/strong>&nbsp;folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nnamespace Vendor\\Extension\\Observer;\n \nuse Magento\\Framework\\Event\\ObserverInterface;\nuse Magento\\Framework\\App\\Response\\RedirectInterface;\nuse Magento\\Checkout\\Model\\Cart;\nuse Magento\\Framework\\Message\\ManagerInterface;\nuse Magento\\Framework\\App\\RequestInterface;\nuse Magento\\Catalog\\Model\\Product;\nuse Magento\\Framework\\App\\Http\\Context as customerSession;\n \nclass Cartadd implements ObserverInterface{\n    protected $cart;\n    protected $messageManager;\n    protected $redirect;\n    protected $request;\n    protected $product;\n    protected $customerSession;\n \n    public function __construct(RedirectInterface $redirect, Cart $cart, ManagerInterface $messageManager,  RequestInterface $request, Product $product, customerSession $session){\n        $this->redirect = $redirect;\n        $this->cart = $cart;\n        $this->messageManager = $messageManager;\n        $this->request = $request;\n        $this->product = $product;\n        $this->customerSession = $session;\n    }\n \n    public function execute(\\Magento\\Framework\\Event\\Observer $observer){\n            $postValues = $this->request->getPostValue();\n            $cartItemsCount = $this->cart->getQuote()->getItemsCount();\n            \/\/your code to restrict add to cart\n            if (condtion) {\n                $observer->getRequest()->setParam('product', false);\n                $this->messageManager->addErrorMessage(__('error msg . '));\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the above method, you can control inventory and set conditions to restrict adding Magento 2 products by the users in the front end<\/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 from adding products to the cart and instead call you to inquire about pricing. The extension will hide the product prices and the \u201cAdd to Cart\u201d button.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Magento folks! I am back with another technical solution on \u2018How to restrict \u201cAdd To Cart\u201d using event in Magento 2 Product management becomes&#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-143","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/143","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=143"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":15630,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions\/15630"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}