1

I want to remove Add To Cart button at category page. but I don't want to use template file. I did it for product view page using event.

    $layout = $observer->getEvent()->getLayout();
    $layout->getUpdate()->addUpdate('<referenceBlock name="product.info.addtocart.additional" remove="true"/>');
    $layout->getUpdate()->load();
    $layout->generateXml();

its working fine; But how can i remove button from category page?

Thanks.

3 Answers 3

1

If we look at this module-catalog\view\frontend\templates\product\list.phtml around line 80 There is function $_product->isSaleable() which then again call to isSalable() function of Magento\Catalog\Model\Product.

There are two events in it catalog_product_is_salable_before and catalog_product_is_salable_after. Depending on your need you can modify it.

I used catalog_product_is_salable_after event to hide Add to Cart button

namespace Namespace\Module\Observer\Frontend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class classname implements ObserverInterface
{
    public $request;

    public function __construct(
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->request = $request;
    }

    public function execute(Observer $observer)
    {
        if ($this->request->getFullActionName() == 'catalog_category_view')
        {

            $product = $observer->getEvent()->getProduct();
            $saleble = $observer->getEvent()->getSalable();

            if($product->getTypeId() == 'customProductType')
            {
                $saleble->setIsSalable(false);
                $salable = $product->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;
                if ($salable && $product->hasData('is_salable')) {
                    $salable = $product->getData('is_salable');
                }
                $product->setIsSaleable($salable);                        
            }
        }
        return $this;
    }
}

Though this will add In Stock or Out of Stock in place of Add to Cart.

1
  • but it also hides product options
    – siddhesh
    Jul 21, 2017 at 14:28
0

You can use core_layout_render_element Event from the Magento\Framework\View\Layout::renderElement() method in order to add an Observer. In the observer you can you preg_replace or analogue to remove generated Add to Cart button.

There is similar question and however event name is outdated: Modify Product Listing 'Add to Cart' button

Hope it helps.

1
  • it's for magento 1 not magento2
    – siddhesh
    Jul 21, 2017 at 14:30
-1

You can achieve this by following CSS tricks.

Add below CSS rules in skin/theme/child/css or skin/theme/default/css

/*add to cart button*/
.products-grid .button.btn-cart {
    display : none ;
}
1
  • This is not suggested as it still renders in frontend and we can access it easily with browser tools. Css has to be used only for client end events such as show/hide etc. Never use to hide something which is restricted for users always. Feb 13, 2018 at 2:07

Your Answer

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

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