Remove add to cart on category product
-
27-02-2021 - |
Question
I am trying to remove the add to cart and add to compare buttons from the category page. At present I have used the following code:
<referenceBlock name="product.info.addtocart" remove="true" />
<referenceBlock name="product.info.options.wrapper.bottom" display="false" />
In my Magento_Catalog/layout/catalog_category_view.xml
without any success
Solution
You need to overwrite following template and remove addtocart content.
vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
Because addtocart content is hard coded. Like:
<div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $position : '' ?>>
<?php if ($_product->isSaleable()): ?>
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* @NoEscape */ $postParams['action'] ?>" method="post">
<input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $postParams['data']['product'] ?>">
<input type="hidden" name="<?= /* @escapeNotVerified */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @escapeNotVerified */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
<?= $block->getBlockHtml('formkey') ?>
<button type="submit"
title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
class="action tocart primary">
<span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
</button>
</form>
<?php else: ?>
<?php if ($_product->isAvailable()): ?>
<div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
Or use css for hiding this block.
For addto block you can remove using xml.Ex:
app/code/SR/MagentoCommunity/view/frontend/layout/catalog_category_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<referenceBlock name="category.product.addto" remove="true"/>
</referenceContainer>
</body>
</page>
OTHER TIPS
add to cart is more of a block so when you want to remove a block you must edit files related to that block like layout.xml file, .phtml file.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange