Yehia A.Salam
Yehia A.Salam

Reputation: 2078

Manipulate Template From Block Magento

I'm trying to change the behavior of the AddtoCart button in the catalog page and show it only in specific cases. What I'm doing is adding a custom module that do the layout updates and replace the Product/List.phtml with my own file. This approach is not very flexible when publishing the custom module, because almost all users will be using a different template file, and they will have to merge mine with theirs.

So my question is can i manipulate the phtml from the block, maybe override *Mage_Catalog_Block_Product_List* and alter the template in the *_BeforeToHtml* function, would this be a better approach, is it doable?

Upvotes: 1

Views: 138

Answers (1)

Joseph at SwiftOtter
Joseph at SwiftOtter

Reputation: 4321

Let me first start by saying that Magento lacks a little flexibility in this area (when rendering to templates). If I am understanding you correctly, you are determining whether the product is salable or not:

  • You can add an event to watch for catalog_product_is_salable_after. With this way, you are working within the system to set whether the product is available to sell or not. When you change the value of the salable Varien_Object in the event, it will change it to be an out of stock message. Maybe that is what you want (but the problem is that it is used in an if/else statement - it's either available to be added to the cart, or it's out of stock (see code below).
  • You could then use a translation file to change Out of Stock to the string that you would like there (kind of a hack).
  • The problem with trying to override Mage_Catalog_Block_Product_List is that the template file is not even included until the _toHtml method. You could override that, and call parent::_toHtml() at the beginning of the code block. However, to remove the add to cart button would involve some very difficult regular expressions.

Here is the block code. It does get a bit sticky.

<?php if($_product->isSaleable()): ?>
    <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>
<?php else: ?>
   <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

Upvotes: 2

Related Questions