سؤال

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>')
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top