4

How to remove the Add to Cart button from Catalog pages only?

3 Answers 3

5

In Magento 2 they've hardcoded them into the catalog product list template.

This can be found around line 80 of app/code/Magento/Catalog/view/frontend/templates/product/list.phtml:

<?php if ($_product->isSaleable()): ?>
    <?php $postParams = $block->getAddToCartPostParams($_product); ?>
    <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
        <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
        <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
        <?php echo $block->getBlockHtml('formkey')?>
        <button type="submit"
                title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                class="action tocart primary">
            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
        </button>
    </form>

A few options exist to suppress them:

  • Create an around plugin for the block and change $_product->isSaleable() to return false for the render of the template, and swap it back afterward
  • Replace this template in your custom theme's layout
  • Hide with CSS

Honestly hiding via CSS isn't the worst option here:

.catalog-category-view .action.tocart { display: none; }
5
  • Hiding via CSS also removed the "Add to Cart" button from product pages. Is there a way to remove it only from Catalog pages? Commented May 4, 2016 at 17:38
  • Ah yes. See the update.
    – philwinkle
    Commented May 4, 2016 at 22:53
  • Can you elaborate fist option plugin around to chang $_product->isSaleable()
    – siddhesh
    Commented Jul 21, 2017 at 14:12
  • Actually I tried to make salable false using catalog_product_is_salable_after event but it gives side effects like it doesn't show product options, it's make product qty 0 and show product as 'out of stock' I would like to do it only for addtocart renderer only.@philwinkle
    – siddhesh
    Commented Jul 21, 2017 at 14:22
  • It can be remove using the layout overide the file in the catalog_product_view.xml file in the theme folder. <referenceBlock name="product.info.addtocart.additional" remove="true" />
    – Divya
    Commented Jul 24, 2019 at 4:50
2

You can always hide it via CSS but i will highly recommend to copy the app/code/Magento/Catalog/view/frontend/templates/product/list.phtml into YourVendor/YourTheme/Magento_Catalog/view/frontend/templates/product/list.phtml and remove all the form code fall in the if ($_product->isSaleable()): condition.

0

app\code\Vendorename\Modulename\etc

di.xml

<?xml version="1.0" encoding="UTF-8"?>

<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="hidecartbutton" type="Vendorename\Modulename\Plugin\Hidecartbutton" sortOrder="1"/>
    </type>
</config>

app\code\Vendorename\Modulename\Plugin

Hidecartbutton.php

<?php

namespace Vendorename\Vendorename\Plugin;

use Magento\Catalog\Model\Product;

class Hidecartbutton
{
    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";
        // $page_arr[] = "catalog_category_view";
        if (in_array($page_name, $page_arr)) {
            return false;
        } else {
            return $product->isSalable();
        }
    }
}

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