I'm building a self contained module which will make modifications to the 'Add to Cart' button through Magento's listing and detail pages. I can easily modify the button on the detail page as it uses it's own template addtocart.phtml.

Where as the listing page has the mark up for these buttons embedded in a chunk of HTML. The code for the button is below.

<?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; ?>

My issue is trying to modify the above code without having to ask the installer to make numerous code changes. Obviously if the site is running a heavily modified template they will need to.

I could provide a new list.phtml template and override the entirety of the file but this seems like it's going to cause more issues then it's worth.

Short of writing some dirty hack to attempt to locate this button and make the modifications is there a better solution? What would you suggest to be the best solution in a contained module as well?

  • 2
    I don't think there's a clean way to do it. Commented May 2, 2014 at 16:48
  • @Tim it's a real shame having to inform customers they're going to have to edit code to get the module to work.
    – Dave
    Commented May 3, 2014 at 19:00

1 Answer 1


Your definition of dirty is a bit much, since each block registers an event that allows you to modify it's HTML after it's processed it itself. And then there's JavaScript as well.

In fact, I'd say the clean way is to use this core_block_abstract_to_html_after event, because rewriting the template to include another template for this button, causes much more overhead. Instead in core_block_abstract_to_html_after you can do a single preg_replace pass, or a maybe somewhat cleaner DOM replacement.

  • This wouldn't be a reliable way of always replacing the button. As it's known for people to edit core templates if they're uncertain of what they're doing. I don't think there is a clean solution that won't require the user to make a number of modifications to their templates. I do like your logic though.
    – Dave
    Commented May 3, 2014 at 18:37
  • Well, let me put it this way. Anyone hacking the template that removes the btn-cart from the class definition is shooting him/herself in the foot. :) So there would be your foothold. Oh, and the feet in this comment are coincidental.
    – user4351
    Commented May 4, 2014 at 14:26
  • You do make a very valid point. Thanks for the response.
    – Dave
    Commented May 6, 2014 at 7:44
  • I ended up using the Zend_Dom_Query() class to allow great modifications to the DOM.
    – Dave
    Commented May 15, 2014 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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