Based on what you've said I'm making the following assumptions
- The tasks requirements prevent you from editting or replacing templates.
- You aren't limited to just using observers and you can override existing classes as long as it is unlikely to require code changes after the module is installed.
Given that these requirements restrict you (more or less) to using the regex approach that you've already set up. We'll need to find some unique value in the product's list item to key off of in order to get product specific information. Normally the item id would be available in the add to cart url, but that isn't the case when a product has required options.
So if you look at the add to cart button that the user will be clicking, you can see that there are only two methods used. One is translate function which is not at all exclusive to the add cart button. The other is getAddToCartUrl()
(documentation here: http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Product_Abstract.html#methodgetAddToCartUrl). I'm not completely certain that this is exclusive to the add to cart buttons, so testing it thoroughly is a good idea.
Now that we've found the getAddToCartUrl
method in the button we want to conditionally remove, and (bonus) it already receives the product object as one of its parameters.
Now we'll want to create a module that rewrites that method. We don't need to change the entire class, so setting up the proper config values and extending the existing class should allow us to have a minimal impact.
Since the change we're looking for only needs to happen on the search pages, I'd suggest wrapping the change in an if block that is specific that controller/action. (how to get those values: http://blog.chapagain.com.np/magento-how-to-get-controller-action-and-module-name-in-template-file/)
Now that we have access to the product object as a parameter of the getAddToCartUrl
and it's wrapped in a condition that only applies the change to the search results page, we just need to write logic to see what category that product belongs to and check if the add to cart button should be visible.
If the result of our logic says that the button should be hidden, we just need to make the method return a unique string that we can easily find with regex and hide the button when that string is found.
TL;DR
Try editing the getAddToCartUrl
method to output something that tells the regex function that's already in place to remove the button.
You are using Mage
class, which has been removed in Magento 2
. The Magento 2
equivalent of
<?php echo Mage::helper('checkout/cart')->getAddUrl($product) ?>
is as below:
Method 1
<?php echo $objectManager->get('Magento\Checkout\Helper\Cart')->getAddUrl($product) ?>
Method 2:
Also, Note that you should avoid using Object Manager
in Magento 2
. It is not a good practice. So, to avoid using Object Manager
you have to write following in your template's Block
File:
protected $_cartHelper;
public function __construct(
\Magento\Checkout\Helper\Cart $cartHelper
) {
$this->_cartHelper = $cartHelper;
}
public function getAddToCartUrl($product, $additional = []) {
return $this->_cartHelper->getAddUrl($product, $additional);
}
For method 2, you can use Magento\Catalog\Block\Product\View
class's getAddToCartUrl
function as your reference.
Best Answer
If we look at this
module-catalog\view\frontend\templates\product\list.phtml
around line80
There is function$_product->isSaleable()
which then again call toisSalable()
function of Magento\Catalog\Model\Product.There are two events in it
catalog_product_is_salable_before
andcatalog_product_is_salable_after
. Depending on your need you can modify it.I used
catalog_product_is_salable_after
event to hideAdd to Cart
buttonThough this will add
In Stock
orOut of Stock
in place ofAdd to Cart
.