How to hide add to cart button for particular products on all page in magento 2
-
09-04-2021 - |
题
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.
解决方案
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.
其他提示
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