3

I want to put the Cart link at the end of the top links - ideally using my local.xml file.

Most top links are added like this:

<reference name="top.links">
      <action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>
</reference>

where I can obviously change the position.

But the cart links seem to be added like this:

<block type="checkout/links" name="checkout_cart_link">
       <action method="addCartLink"></action>
       <action method="addCheckoutLink"></action>
</block>

How do I change the position of a link that doesn't use the addLink method?

4

The position is hardcoded in /app/code/core/Mage/Checkout/Block/Links.php:

$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');

The cart is pos 50 and the checkout link is pos 60. So maybe the easiest way is to position all top.links below 50. But to place just the cart link at the end you would have to rewrite the addCartLink() method.

  • Perfect - so obvious now that you point it out. – hammygoonan Jul 11 '13 at 11:11
0

Another way of doing it is by creating a small module (available here: https://github.com/digitalpianism/easytoplinks)

app/etc/modules/DigitalPianism_EasyToplinks.xml

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

app/code/local/FactoryX/EasyToplinks/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <DigitalPianism_EasyToplinks>
            <version>0.0.5</version>
        </DigitalPianism_EasyToplinks>
    </modules>
    <global>
        <blocks>
            <page>
                <!--
                Mage_Page_Block_Template_Links
                -->
                <rewrite>
                    <template_links>DigitalPianism_EasyToplinks_Block_Page_Template_Links</template_links>
                </rewrite>
            </page>
        </blocks>
    </global>
</config>

app/code/local/FactoryX/EasyToplinks/Block/Page/Template/Links.php

<?php

/**
 * Class DigitalPianism_EasyToplinks_Block_Page_Template_Links
 */
class DigitalPianism_EasyToplinks_Block_Page_Template_Links extends Mage_Page_Block_Template_Links
{

    /**
     * Change the position of an existing to make the layout customization easier
     * @param string $url
     * @param int $position
     * @return $this
     */
    public function setPosition($url, $position)
    {
        // Get the link and delete it from the current position
        foreach ($this->_links as $k => $v) {
            if ($v->getUrl() == $url) {
                $link = $this->_links[$k];
                unset($this->_links[$k]);
            }
        }

        if (isset($link)) {
            // Add link to new position
            $this->_addIntoPosition($link, $position);
        }

        return $this;
    }
}

Now that you have this module you can change the position easily via the layout by using:

<reference name="top.links">
    <action method="setPosition">
        <url>checkout/cart</url>
        <position>99</position>
    </action>
</reference>

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.