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

2 REPLIES 2

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.