How To Hide Add To Cart Button in Magento 2
Magento 2 stores allow the users to add products to the cart for checkout. However, there may be scenarios where the store owner may want to restrict this action. Disabling add to cart button in Magento 2 store can restrict the users to add products to the cart.
There are several reasons for doing so. For example,
- Hide add to cart button for upcoming productsYou are working on some products or software. But you want to start having the SEO benefit of the keyword and not yet actually offer the product. At that time, you can offer the products but disable the add to cart button. Doing so, you can create the hype and also restrict the users to add them to the cart!
- Hide add to cart button for out of stock productsIf you are selling products that are currently out of stock and people are looking for it, you can implement the below solution. And also, you can allow them to subscribe to that product for a notification when the product gets back in stock with further customization. Hence, you won’t lose the conversion too!
- Hide add to cart button for specific productsProducts that are only sample products or need to not be sold at the moment, implement the below solution.
Before:
After:
The below method to hide add to cart button in Magento 2 disables it from product page as well as the category page.
The solution is compatible with the latest Magento 2.4.6 version. So, make sure to download the latest Magento version and implement this solution.
Method to Hide Add To Cart button in Magento 2:
- Create di.xml file in Vendor\Module\etc and copy the below code:
123456<?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\HideButton" sortOrder="1"/></type></config> - Create HideButton.php file in Vendor\Module\Plugin and copy the below code:
123456789<?phpnamespace Vendor\Module\Plugin;use Magento\Catalog\Model\Product;class HideButton{public function afterIsSaleable(Product $product){return [];}} - Run php bin\magento cache:clean and refresh the page add to cart button will be vanished from category and product pages.
That’s it.
Or, you may opt for Meetanshi’s Magento 2 Call for Price extension to hide the “Add to Cart” button and product prices on the front end. The visitors can call you for pricing inquiries and thus improve the quotation system in your Magento 2 store.
Any doubts on how to remove add to cart button in Magento 2 store can be mentioned in the Comments section below and I’ll be happy to help.
Feel free to share the solution with fellow developers via social media.
Thanks.
Sanjay 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.
12 Comments
Will this hide add to cart for all products or only specific products which are out of stock?
Hello Gaurav,
You can set the condition for product-wise in the plugin file as per your requirement.
I hope it helps.
Hi,
How could we remove the add to cart button only on product list pages and not product pages?
Thank you.
Hello Ben,
Enable the template path hints in Magento 2 store frontend. Find list.phtml file from where it is called and override it in the theme. Comment or remove the “Add to Cart” button.
Thanks.
In the article you’ve mentioned this “Hide add to cart button for specific productsProducts that are only sample products or need to not be sold at the moment, implement the below solution”.
How do we use this code/module to implement it only for a specific category instead of all products ?
Hello Jake,
In the function afterIsSaleable() you can use your own code to hide add to cart button for a specific category.
Thank you.
I don’t have a vendor/module folder from my root magento 2 directory?
Hello Alex,
You need to develop a custom extension and use the above two files in it. Or, if you are already using a custom module, use the above code in it.
Thank you.
This solution, not work Magento 2.3.4
Hello,
Thank you for drawing my attention.
I’ve updated the code to make the solution compatible with the latest Magento 2 versions.
Please check it and let me know if you have further doubts.
Thank you.
Thanks for sharing this solution. I will implement and test it.
Keep up the good work!
Thank you very much!