Magento, Magento 2

How to Hide Add to Cart Button for Specific Product in Magento 2

In this tutorial, Today I will explain how to hide add to cart button for the specific product in Magento 2. Whenever the Store owners want to restrict add to cart button for a specific product for a few days at that time we need to hide add to cart button.

Sometimes, When upcoming products publish on the store before launch at that time, the add to cart button should be hidden. In addition, When the product will be out of stock at that time, also we need to hide add to cart button.

So, Let’s see how to hide add to cart button programmatically for the specific product

You may also like this :

1) Let’s assume that you have created a simple module. Create di.xml file at app/code/RH/Helloworld/etc/ and paste the below code :


<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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="hide_button" type="RH\Helloworld\Plugin\HideBtn" sortOrder="1" />
    </type>
</config>

2) Now, Create HideBtn.php file at app/code/RH/Helloworld/Plugin/ and paste the below code :


<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Plugin;

class HideBtn
{               
    public function afterIsSaleable(\Magento\Catalog\Model\Product $product)
    {
        if($product->getId() == 1)
        {
            return false; // For hide button
        } else {
            return true; // For display button
        }
    }
}

Here, you can set any of the product ids on which you want to hide the add to cart button.

That’s it !!!\

Output :

Category Page Button Remove

Product View Hide Button

I hope this blog is easy to understand how to hide add to cart button for the specific product in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with a proper solution.

Stay Safe and Stay Connected !!

Tagged ,