3

The company I work for provide a free extension to implement a common B2B requirements in a Magento shop (this is not a plug but a cry for help honest). The request has come in to provide a system config option to remove the Add to cart button, sounds simple enough as we already do this for other items such as price and cart links.

Let me explain a little bit of how I have tried to solve this so far.

  1. We have provided the user with the option via the admin section including config options as to how to identify there Add to cart button. This defaults to the magento standard template information but should allow for different themes

    • You can select the type of html element (div/a/button etc.)

    • You can select to to identify by id or class

    • You can enter the value of the id or class

  2. Then using an observer on the event core_block_abstract_to_html_after and block Mage_Catalog_Block_Product_List (we do check more blocks) we build a regular expression which gets run over the html and replaces the button with some helpful text.

This works "well", but the problem I have is that our module can be activated on a category level. Obviously this means that you could have a search result page with both active and inactive categories. Currently our approach will just remove all Add to cart buttons in this block even on active products as it can not check on a product basis.

Our module code can be found at https://github.com/sitewards/B2BProfessional/tree/add-to-cart if you want to take a quick look. I am happy to answer more questions if people have any suggestions.

5

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.

  • Great stuff! I followed your idea and this is now working well. – David Manners Apr 19 '13 at 12:16
  • Glad to help. It's definitely not a standard approach, but we don't always have ideal deployment conditions. – Sam Apr 19 '13 at 15:03
3

Why not just add a boolean attribute to the product (show_add_to_cart) - then rewrite the isSaleable() method to include checking the value of said attribute.

That way, it would pretty much be native to deny cart addition .

  • 1
    +1 - I like this method outside of the traditional layout xml remove. – philwinkle Apr 18 '13 at 20:34
  • This was also my first approach but in the product/list.phtml this would then display the out of stock message which is not the desired outcome. – David Manners Apr 19 '13 at 6:39
1

I had gone through link given in first question from all above but that link is not available right now .So as you have mentioned that you want to hide add to cart button for specific product as per my suggestion you can use this free and excellent extension for your requirement.

Remove add to cart button free extension

0

In Design > Custom Layout XML for the products you desire to remove:

<reference name="content">
  <remove name="product.info.addtocart"/>
</reference>

If you want to remove from the product list on category pages, you need to modify your template - remove this block from template/catalog/product/list.phtml:

<?php if($_product->isSaleable()): ?>
    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
<?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

Make sure to copy this file to your theme.

  • Sadly this would only remove the button on the product view page. The problem I am having is in product/list.phtml – David Manners Apr 19 '13 at 6:41
  • See my edit above. – philwinkle Apr 19 '13 at 14:18
  • @philwinkle without editing list.phtml. how to remove add to cart button?magento.stackexchange.com/q/98188/1927 – MageDev Jan 21 '16 at 13:34
  • In later versions of Magento this can be done via layout – philwinkle Jan 21 '16 at 14:43

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.