cancel
Showing results for 
Search instead for 
Did you mean: 

Hide Add to Cart for Products in Specific Category

Hide Add to Cart for Products in Specific Category

I want to create a module, which hides the Add to Cart button for all products in the category "archived" - catalog view and product view. It also can be an attribute instead of category.

With the help of www I created the following files.

 

app/code/eyetec/hideadd2cart/registration.php

<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    '2EyeTec_HideAdd2Cart',
    __DIR__
);

 

app/code/eyetec/hideadd2cart/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="2EyeTec_HideAdd2Cart" schema_version="0.0.1" setup_version="0.0.1"></module>
</config>

I got the module enabled and can see it in the app/etc/config.php - so I guess these two files are correct!

 

This is the php file - unfortunately it's not working. Can somebody explain to me, why?

 

I know that the condition is referring the product id (thought its easier), how can I use the category in the condition?

 

app/code/eyetec/hideadd2cart/Plugin/HideButton.php

<?php
namespace eyetec\hideadd2cart\Plugin;
use Magento\Catalog\Model\Product;
class HideButton
{
	public function afterIsSaleable(Product $product)
    {
      	if($product->getId() == 1)
        { 
        	return true; // For display button
        } else { 
            return false; // For hide button
        } 
    } 
}

I'm using Magento 2.4.4

3 REPLIES 3

Re: Hide Add to Cart for Products in Specific Category

@quickpayportal wrote:

I want to create a module, which hides the Add to Cart button for all products in the category "archived" - catalog view and product view. It also can be an attribute instead of category.

With the help of www I created the following files.

 

app/code/eyetec/hideadd2cart/registration.php

<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    '2EyeTec_HideAdd2Cart',
    __DIR__
);

 

app/code/eyetec/hideadd2cart/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="2EyeTec_HideAdd2Cart" schema_version="0.0.1" setup_version="0.0.1"></module>
</config>

I got the module enabled and can see it in the app/etc/config.php - so I guess these two files are correct!

 

This is the php file - unfortunately it's not working. Can somebody explain to me, why?

 

I know that the condition is referring the product id (thought its easier), how can I use the category in the condition?

 

app/code/eyetec/hideadd2cart/Plugin/HideButton.php

<?php
namespace eyetec\hideadd2cart\Plugin;
use Magento\Catalog\Model\Product;
class HideButton
{
	public function afterIsSaleable(Product $product)
    {
      	if($product->getId() == 1)
        { 
        	return true; // For display button
        } else { 
            return false; // For hide button
        } 
    } 
}

I'm using Magento 2.4.4


Make sure your attribute is available on product listing page by setting "Used in Product Listing" to "Yes" when editing your attribute.

Re: Hide Add to Cart for Products in Specific Category

Hey, @christian_meyer,

 

I have seen your problem. you can hide Add to cart for a specific category. Please refer to this blog: https://magecomp.com/blog/hide-add-to-cart-button-in-magento-2/

 

This blog definitely help you, else you can use the Hide price extension. This also helps you to hide the Add-to-cart button. 

 

Hope this helps you. 

 

Thank you. 

 

Re: Hide Add to Cart for Products in Specific Category

Hi @christian_meyer,

 

To hide the "Add to Cart" button for products in a specific category using an afterIsSalable plugin, you need to check if the product belongs to the specified category. Here’s how you can achieve this: 

 

Step-by-Step Implementation

 

  • Update 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>
  • Create the Plugin Class:

In your Plugin/Product.php file, add the logic to check if the product belongs to a specific category.

<?php
namespace Vendor\Module\Plugin;
use Magento\Catalog\Model\Product as ProductModel;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class Product
{
    protected $categoryRepository;
    public function __construct(CategoryRepositoryInterface $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

    public function afterIsSaleable(ProductModel $product, $result)
    {
        // Array of category IDs for which to hide the "Add to Cart" button
        $categoriesToHide = [/* Category IDs */];

        // Get product categories
        $categoryIds = $product->getCategoryIds();

        // Check if product belongs to any of the specified categories
        foreach ($categoryIds as $categoryId) {
            if (in_array($categoryId, $categoriesToHide)) {
                return false; // Hide "Add to Cart" button
            }
        }

        return $result; // Use the original result if the product is not in the specified categories
    }
}

 

If the issue will be resolved, Click Kudos & Accept as a Solution.