5

What is the proper way to override a method, for example, the Mage_Checkout_Block_Links::addCartLink() method.

My goal is to add some HTML before the Mage_Checkout_Block_Links::addCartLink() method. However, I don't know where to begin or which file to copy and edit under my custom theme.

I am thinking about something like this:

public function addLink($beforeText='<i class="icon-shopping-cart"></i>')
7

Here is a small tutorial on how to override a block a model or a helper: http://magedev.com/2009/06/03/magento-overriding-model-block-or-helper/ Keep this link save...you will need it in the future. Now for your problem...following the tutorial in the link above Create your own extension. Lets' call it 'Easylife_Checkout'. You will need to following files: app/etc/modules/Easylife_Checkout.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Checkout>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Checkout />
            </depends>
        </Easylife_Checkout>
    </modules>
</config>

app/code/local/Easylife/Checkout/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Checkout>
            <version>0.0.1</version>
        </Easylife_Checkout>
    </modules>
        <global>
        <blocks>
            <checkout>
                <rewrite>
                    <links>Easylife_Checkout_Block_Links</links>
                </rewrite>
            </checkout>
        </blocks>
        </global>
</config>

and app/code/local/Easylife/Checkout/Block/Links.php

<?php
class Easylife_Checkout_Block_Links extends Mage_Checkout_Block_Links{
    public function addCartLink()
    {
        $parentBlock = $this->getParentBlock();
        if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
            $count = $this->getSummaryQty() ? $this->getSummaryQty()
                : $this->helper('checkout/cart')->getSummaryCount();
            if ($count == 1) {
                $text = $this->__('My Cart (%s item)', $count);
            } elseif ($count > 0) {
                $text = $this->__('My Cart (%s items)', $count);
            } else {
                $text = $this->__('My Cart');
            }

            $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
            $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"', '<i class="icon-shopping-cart"></i>');//this will add the link. The last parameter is the 'before text'. You can add an other parameter after that that means $afterText
        }
        return $this;
    } 
}

Clear the cache and you should be done.

  • I appreciate this very, very much! – Raphael Rafatpanah Apr 16 '13 at 15:55
  • app/code/local/Easylife ... Are you sure that is the correct path? App/code does not have a 'local' folder but there is app/locale/ .... Should I create the local folder inside code? – Raphael Rafatpanah Apr 16 '13 at 16:16
  • yeah...the 'app/code/local' folder does not exist. Just create it. – Marius Apr 16 '13 at 16:19
  • I very carefully created these files exactly as posted and inserted them to the correct folders. I cleared the cache. I inspected the HTML with Chrome's Inspect Element tool, and I see no changes. I learned about how magento uses the local folder to override files located in the core folder. Is there a way to test to see if these files are being used? – Raphael Rafatpanah Apr 16 '13 at 16:28
  • 1
    It depends on what you are trying to override. In the tutorial a model was overwritten. You needed to override a block. In the same way you can override a helper. – Marius Apr 16 '13 at 18:07

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.