🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Set Conditions to Restrict Adding Products in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 2 min read

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

1. Create event.xml in the etc folder

<?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>

2. Create Cartadd.php file under the Observer folder

<?php
 
namespace 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 cart
            if (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 from adding products to the cart and instead call you to inquire about pricing. The extension will hide the product prices and the “Add to Cart” button.

Thanks

Sanjay Jethva Full Image
Article bySanjay 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.