0

I have tried to remove the Add to cart button with many solutions out there, but none has worked. I'm using this solution: How to remove the Add to Cart button from Catalog pages in Magento 2?.

Also tried this one but the products were disabled, not the add to cart button: How to disable add to cart in magento 2

What I need to do, is something very simple, I just need to check in an if() condition if the ProductID of the product is different than 2 ids, so customer won't be able to add to cart products that are not the ones that can be bought.

Something like this is what I need to accomplish.

$productId = $product->getId();

if($productId != "2078" || $productId != "2079"){
    //disable the add to cart button of the rest of products
}
else{
//products ID are 2078 or 2079
return true;
}

Greetings!

0

2 Answers 2

1

You can also do by creating product attribute named "show add to cart" with yes/no option. You can set yes option by default and set no to those products that won't be able to show add to cart button and instead of that Id condition check with attribute value. It's good to keep product attribute when doing this kind of development rather to use product Id.

4
  • how can I do this? I have tried to obtain my attribute like this <?php if ($_product->getDisableAddToCart()): ?> but it's not working, and how can I verify it 's yes or no in the option? thanks! Commented May 19, 2020 at 18:41
  • When you will create attribute at that time there is an option to set default value. Set it yes . You can get the values on product page by $product->getData("attribute name")
    – Sheba
    Commented May 19, 2020 at 18:57
  • Hi, I have done that but it's not respecting Yes/No option, I just have if($product->getData("attribute name")) but it does not matter If I have Yes or No, it's always like No, I create it with Yes as default value. Commented May 19, 2020 at 19:08
  • it's working now! I change default value to No. Commented May 19, 2020 at 19:13
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
{

    public function afterIsSaleable(Product $product, $isSaleable)
    {
        $prd_id = $product->getId();
        if (($prd_id != 2078) || ($prd_id != 2079)) {
            return false;
        } else {
            return $product->isSalable();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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