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.

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.

  • it's for magento 1 not magento2 – siddhesh Jul 21 '17 at 14:30
0

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.

  • but it also hides product options – siddhesh Jul 21 '17 at 14:28
-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 ;
}
  • 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. – Kalyan Chakravarthi V Feb 13 '18 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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