How to Set Conditions to Restrict Adding Products in Magento 2
Hello Magento folks!
I am back with another technical solution on ‘How to restrict “Add To Cart” using event in Magento 2’.
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 Set Conditions to Restrict Adding Products in Magento 2 and work on cart conditions and events.
Here, I’ve 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.
Let’s begin with the solution to restrict ‘add to cart’ in Magento 2 using conditions.
Steps to Set Conditions to Restrict Adding Products in Magento 2:
- Create event.xml in etc folder
1234567<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"><event name="controller_action_predispatch_checkout_cart_add"><observer name="restrict_sales_model_cart_add_before" instance="Vendor\Extension\Observer\Cartadd"/></event></config> - Create Cartadd.php file under the Observer folder
123456789101112131415161718192021222324252627282930313233343536373839<?phpnamespace Vendor\Extension\Observer;use Magento\Framework\Event\ObserverInterface;use Magento\Framework\App\Response\RedirectInterface;use Magento\Checkout\Model\Cart;use Magento\Framework\Message\ManagerInterface;use Magento\Framework\App\RequestInterface;use Magento\Catalog\Model\Product;use Magento\Framework\App\Http\Context as customerSession;class Cartadd implements ObserverInterface{protected $cart;protected $messageManager;protected $redirect;protected $request;protected $product;protected $customerSession;public function __construct(RedirectInterface $redirect, Cart $cart, ManagerInterface $messageManager, RequestInterface $request, Product $product, customerSession $session){$this->redirect = $redirect;$this->cart = $cart;$this->messageManager = $messageManager;$this->request = $request;$this->product = $product;$this->customerSession = $session;}public function execute(\Magento\Framework\Event\Observer $observer){$postValues = $this->request->getPostValue();$cartItemsCount = $this->cart->getQuote()->getItemsCount();//your code to restrict add to cartif (condtion) {$observer->getRequest()->setParam('product', false);$this->messageManager->addErrorMessage(__('error msg . '));}}}
With the above method, you can control inventory and set conditions to restrict adding Magento 2 products by the users in the front end!
Or, you could simply use the Magento 2 Call for Price extension to restrict customers to add products to the cart and instead call you for inquiring about pricing. The extension will hide the product prices and the “Add to Cart” button.
Let me know your doubts and questions in the comments section. Also, let me know how this code has helped and which conditions you have set to restrict adding Magento 2 products.
Don’t forget to rate the post with 5 stars!
Thanks 🙂
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Get Order Data From Magento 2 “sales_order_place_after” Event
How to Sort Magento 2 Category Products by Stock Quantity
Next