0

How to hide add to cart button for particular products on all page in magento 2

To hide add to cart button all page using a below plugin:

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="hidebutton" type="Vendor\Module\Plugin\HideButton" sortOrder="10" disabled="false"  />
    </type>
</config>

HideButton.php

 <?php

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

    class HideButton
    {
        public function afterIsSaleable(
        \Magento\Catalog\Model\Product $subject,
        $result
    ) {
       if($subject->getId() == 36)
       {
         return false;
       }
       else
       {
        return  true;
       }

    }
    }

Above plugin works well but its shows an "Out of Stock" instead of "Add to cart" in listing and home page how to hide a "out of stock" text.

enter image description here

9
  • You can add attribute and manage yes or no and manage it on that product page. okay Commented Feb 19, 2020 at 6:48
  • i tried that way already as you said above. its only hide in product details and listing page but i need to hide in all page like home page, wishlish page , compare list page etc Commented Feb 19, 2020 at 6:57
  • you need to put that in all pages. Commented Feb 19, 2020 at 6:58
  • have you using any theme? Commented Feb 19, 2020 at 6:59
  • here is you focusing Out of stock button. and you mentioned other buttons in questions. Commented Feb 19, 2020 at 6:59

2 Answers 2

5

Try to use afterIsSalable() plugin using this below way :

app/code/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="hidebutton" type="Vendor\Module\Plugin\Product" sortOrder="1" />
    </type>

</config>

app/code/Vendor/Module/Plugin/Product.php

<?php
namespace Vendor\Module\Plugin;

class Product
{               
    public function afterIsSaleable(\Magento\Catalog\Model\Product $product)
    {
        if(your_condition_check)
        {
            return true; // For display button
        }else {
            return false; // For hide button
        }
    }
}

Clean cache and check it.

7
  • S, its work well its hide a add to cart button in all page but its show a "Out of stock" instead of "Add to cart" button, now my question is how to hide a out of stock text Commented Feb 19, 2020 at 7:20
  • 2
    You used wrong after plugin function. Please check my function name. Commented Feb 19, 2020 at 7:24
  • Thank you so much for a intimate a bug. now its works fine. Commented Feb 19, 2020 at 7:30
  • Happy to help !! Happy Coding (y) Commented Feb 19, 2020 at 7:31
  • @RohanHapani Works great. However, is it possible to return anything else ("Please login") or something like this when button is hidden?
    – zlep
    Commented Mar 29, 2021 at 9:40
0

This is can be done by Override addtocart.phtml

https://magenticians.com/magento-2-disable-add-to-cart-button/

I hope this link is what you are looking for

6
  • Thank you for your suggestion but i don't want to override a templates i need to done by without overriding a template Commented Feb 19, 2020 at 6:52
  • i used a plugin as you post above. but its show "Out of stock" instead of "Add to Cart" button. i need to show only blank space not a add to cart button or out of stock text Commented Feb 19, 2020 at 7:11
  • you can disable it by going into Stores->Configure->From Catalog drop-down select 'Inventory'->uncheck and choose no for Display Out Of Stock Products'
    – Mr Zid
    Commented Feb 19, 2020 at 7:13
  • its already default set as no only refer it prnt.sc/r4fkkr Commented Feb 19, 2020 at 7:16
  • Try this also . Go to admin > Stores > configuration > Catalog > Inventory > Product Stock Options Set Backorders to "No Backorders"
    – Mr Zid
    Commented Feb 19, 2020 at 7:20

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.