Glad to see you again in Magento blog! Lately I have got a few messages asking me about how to hide or remove Add to Cart button from product page based on some conditions of product. I think there have been many other people having the same question and interested in the best solution for this issue. Thus I decide to write this short Magento tutorial. Although Magento does not support this feature, you can still implement it without modifying Magento core by taking some following steps:
- Create a new module by using Module Creator
- Insert a new event observer into config file of that module (app/code/local/Namespace/Modulename/etc/config.xml). Paste this code below into <frontend> tab.
<events> <catalog_product_is_salable_after> <observers> <modulename> <type>singleton</type> <class>modulename/observer</class> <method>catalog_product_is_salable_after</method> </modulename> </observers> </catalog_product_is_salable_after> </events>
- Create model Observer in your module
(app/code/local/Namespace/Modulename/Model/Observer.php)
class Namespace_Modulename_Model_Observer { public function catalog_product_is_salable_after($observer) { $product = $observer->getProduct(); $salable = $observer->getSalable(); if($product->getSku() == 'abcxyz'){ /* product condition */ $salable->setIsSalable(false); } return $this; } }
After installing this module on your store, the Add to Cart button will be removed. It’ll look like the picture below:
Finally, please note that you need to replace Namespace and Modulename in the example above with the specific values.
Hope my post will be helpful for you. Don’t hesitate to leave comments if you have any other ideas or solutions for this issue. Nice code!
Related:
- Configure server to install Magento
- Submit Magento Products into shopping cart sites
- Magento Plugins and Magento 2 Plugins
1 Comment
thanks. it is very helpful for me