Nil
Nil

Reputation: 1193

Magento add id attribute to top links

I want to add id attribute to all top links. I got one solution as below

<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><liParams>id="myaccount"</liParams></action>

Adding liParams works for My Account link only this solution not works for My Cart and Checkout links.

Upvotes: 1

Views: 1955

Answers (3)

Rohit
Rohit

Reputation: 61

addCartLink() function is used to create top cart link in magento. Either rewrite Mage_Checkout_Block_Links class and change addCartLink() function as follows or simply copy app/code/core/Mage/Checkout/Block/Links.php to app/code/local/Mage/Checkout/Block/Links.php and edit

 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->__('Cart (%s)', $count);
        } elseif ($count > 0) {
            $text = $this->__('Cart (%s)', $count);
        } else {
            $text = $this->__('Cart (0)');
        }

        $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
        $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, 'id="top_cart"', 'class="top-link-cart"'); //add your custom class or id here
    }
    return $this;
}

Upvotes: 0

Slimshadddyyy
Slimshadddyyy

Reputation: 4073

Checkout Link uses below function located at app/code/core/Mage/Checkout/Block/Links.php

public function addCartLink()
    {
        ////.....
        $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
        return $this;
    }

You can see that it does not contains any <liParams> or <aParams>. While the addLink() method located at Mage_Page_Block_Template_Links contains the params as shown below

public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
        $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')

Thus you can do either of the following

  1. Override the addCartLink() in your local and add params as needed

  2. Remove the checkout link and then again add it with explicit URL and params

For example in your local.xml, to add a custom link to Top Links via local.xml:

<reference name="top.links">
    <action method="addLink" translate="label title">
        <label>My Link</label>
        <url>path/to/page</url>
        <title>My link tooltip</title>
        <prepare>true</prepare>
        <urlParams/>
        <position>150</position>
        <liParams>id="my-custom-id"</liParams>
    </action>
</reference>

Upvotes: 3

Amit Bera
Amit Bera

Reputation: 7611

@Nilesh Yadav,cart and checkout link are use different methods for create top link

<action method="addCartLink"></action>
   <action method="addCheckoutLink"></action>

Other used addLink function

Checkout and Cart use class Mage_Checkout_Block_Links function addCheckoutLink and addCartLink

Modify xml code

  <reference name="top.links">
            <block type="checkout/links" name="checkout_cart_link">
                <action method="addCartLink"><liParams>id="my-custom-id"</liParams></action>
                <action method="addCheckoutLink">  <liParams>id="my-custom-id"</liParams></action>
            </block>
        </reference>

and Copy app/code/core/Mage/Checkout/Block/Links.php to

app/code/local/Mage/Checkout/Block/Links.php

goto function modify logic in addCartLink

 public function addCartLink($liparams=null)
 {
.....
if(is_null())
{ $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
}else
{
$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, $liparams, 'class="top-link-cart"');
}

....

Also

  public function addCheckoutLink($liparams=null)
    {
....
        if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
            $text = $this->__('Checkout');
        if(is_null()){
            $parentBlock->addLink(
                $text, 'checkout', $text,
                true, array('_secure' => true), 60, null,
                'class="top-link-checkout"'
            );
    }else{
            $parentBlock->addLink(
                $text, 'checkout', $text,
                true, array('_secure' => true), 60, $liparams=null,
                'class="top-link-checkout"'
            );

        }
        }
..
    }

Upvotes: 3

Related Questions