Sometimes store admin wants to not display add to cart button on the product detail page that’s why in this blog, we will discuss how to hide add to cart button from the product page by the plugin.
Follow these Steps to Hide Add To Cart Button in Magento 2:
Step 1: Create registration.php file
1 |
app/code/Webiators/HideAddToCartBtn/registration.php |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /** * @category Webiators * @package Webiators_HideAddToCartBtn * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webiators_HideAddToCartBtn', __DIR__); |
Step 2: Create module.xml file
1 |
app/code/Webiators/HideAddToCartBtn/etc/module.xml |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" ?> <!-- /** * * @category Webiators * @package Webiators_HideAddToCartBtn * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webiators_HideAddToCartBtn" setup_version="1.0.0"/> </config> |
Step 3: Create a plugin HideAddToCartButton.php file
app/code/Webiators/HideAddToCartBtn/Plugin/HideAddToCartButton.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Webiators\HideAddToCartBtn\Plugin; use Magento\Catalog\Model\Product; class HideAddToCartButton { protected $request; public function __construct( \Magento\Framework\App\Request\Http $request ) { $this->request = $request; } public function afterIsSaleable(Product $product, $isSaleable) { // get product data $page_name = $this->request->getFullActionName(); $page_arr = []; $page_arr[] = "catalog_product_view"; if (in_array($page_name, $page_arr)) { return false; } else { return $product->isSalable(); } } } |
Step 4: Create a di.xml file and call the plugin in this file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0"?> <!-- /** * * @category Webiators * @package Webiators_HideAddToCartBtn * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ --> <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="hideaddtocartbutton" type="Webiators\HideAddToCartBtn\Plugin\HideAddToCartButton" sortOrder="1"/> </type> </config> |
Step 5: Now you need to run command:
php -dmemory_limit=5G bin/magento setup:upgrade
php -dmemory_limit=5G bin/magento setup:di:compile
php -dmemory_limit=5G bin/magento setup:static-content:deploy -f en_US
php -dmemory_limit=5G bin/magento cache:flush
php -dmemory_limit=5G bin/magento cache:clean
php -dmemory_limit=5G bin/magento indexer:reindex
Step 6: Let’s open the product page and see the result:
Thank You!