• Magento 2 clean installation with Luma.
  • We shouldn't use templates.
  • The replacement needs to create a popup form related to product data to send and save it to my grid.

I'm searching for a way to replace the add to cart button from a product in the Magento catalog with my condition. I found the only way to hide the button with JS and set my button instead, but it's not a "clean" way and I know that we can do it more correctly, probably with an observer or plugin.

1 Answer 1


To prevent the product from Adding to cart you can use below code: app/code/Vendor/Extension/etc/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="prevent_add_to_cart" instance="Vendor\Extension\Observer\PreventAddToCart" />

Create a observer file under: app/code/Vendor/Extension/Observer/PreventAddToCart.php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Checkout\Model\Session;

class PreventAddToCart implements ObserverInterface
    protected $messageManager;
    protected $redirect;
    protected $_checkoutSession;

    public function __construct(
        ManagerInterface $messageManager,
        Session $checkoutSession,
        RedirectInterface $redirect
    ) {
        $this->messageManager = $messageManager;
        $this->redirect = $redirect;
        $this->_checkoutSession = $checkoutSession;

    public function execute(Observer $observer)
        // Your custom logic to prevent adding to cart
        $product = $observer->getEvent()->getProduct();

Also to Hide Add to cart button use plugin code as below:

Create a di.xml file - Vendor\Module\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="hideaddtocart" type="Vendor\Extension\Plugin\Hideaddtocart" sortOrder="32"/>

Then, create Vendor\Module\Plugin\Hideaddtocart.php

namespace Vendor\Extension\Plugin;
use Magento\Catalog\Model\Product;

class Hideaddtocart
    public function afterIsSaleable(Product $product)
        return [];

I hope this answer finds helpful.

  • I know this approach and it's helpful but only in case we need to remove the button, what about replace? Feb 7 at 10:26
  • Replace the button with what ?
    – user3411
    Feb 7 at 10:27
  • Let's say replace the add to cart button with a custom HTML code - let it be just a simple <button></button>. Feb 7 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.