How to Check Condition before Product Add to Cart?

  • author-img Nidhi Arora
  • 7 years
how-to-check-condition-before-product-add-to-cart

Sometimes it is beneficial to restrict the usage of ‘Add to Cart’ button on your Magento 2 based website. Yes, you heard that absolutely right. It would be beneficial if you alternatively display a generic message, Login button, subscription box, sign up form, etc., to your users, when they click on ‘Add to Cart’ button on your Magento 2 site. It will force your users to read or complete the displayed condition and then they can add a product to cart.

But, it is quite difficult to check that condition before product added to cart. That’s why we have come up with another “How to” blog on Magento 2. Read on to learn how to check condition before product added to cart:

Here we will use controller_action_predispatch_checkout_cart_add observer. Now, you can follow the steps as mentioned below:

Step 1:

Generate events.xml file in app/code/NameSpace/ModuleName/etc/frontend location and add the code mentioned below:

<?xml version=”1.0″?>

<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:noNamespaceSchemaLocation=”urn:magento:framework:Event/etc/events.xs  d”>

<event name=”controller_action_predispatch_checkout_cart_add”><observer name=”yourcustomname”
instance=”NameSpace\ModuleName\Observer\RestrictAddToCart” /></event>

</config>

Step 2:

Next step is to create a class where you can check condition and restrict ‘Add To Cart’.

Make RestrictAddToCart.php file at NameSpace\ModuleName\Observer\ location

<?php

namespace NameSpace\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class RestrictAddToCart implements ObserverInterface

{

/**

* @var \Magento\Framework\Message\ManagerInterface

*/

protected $_messageManager;

/**

* @param \Magento\Framework\Message\ManagerInterface $messageManager

*/

public function __construct(

\Magento\Framework\Message\ManagerInterface $messageManager

)

{

$this->_messageManager = $messageManager;

}

/**

* add to cart event handler.

*

* @param \Magento\Framework\Event\Observer $observer

*

* @return $this

*/

public function execute(\Magento\Framework\Event\Observer $observer)

{

if (/* your condition */) {

$this->_messageManager->addError(__(‘your custom message’));

//set false if you not want to add product to cart

$observer->getRequest()->setParam(‘product’, false);

return $this;

}

return $this;

}

}

Step 3:

And that’s it! You can clean or flush your magento2 cache. And, this ‘how to’ will work for your users on your Magento 2 based website. Now, when your users will click on ‘Add to Cart’ button, it will first check the condition (which is implemented for users) and then carry on according to that condition.

Hope it helps you. If you still need a helping hand, Envision Ecommerce’s team of certified Magento developers are here to help you. Contact us today for any Magento 2 related query.

Download Blog

ENQUIRY

Ready to Get Started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    815 Brazos St STE 500, Austin,
    TX 78701, USA