1

I would like to add a class to the <li> and <a> of the My Cart link in the top.links menu.

Other solutions online show to remove My Cart:

<remove name="checkout_cart_link"/>

And then re-add My Cart using the addLink method:

        <action method="addLink" translate="label title">
            <label>My Cart</label>
            <url>/checkout/cart</url>
            <title>My Cart</title>
            <prepare />
            <urlParams/>
            <position>3</position>
            <liParams>
                <class>my-cart-list-item</class>
            </liParams>
            <aParams>
                <class>my-cart-anchor</class>
            </aParams>
            <beforeText />
            <afterText />
        </action>

This technically works as it does place the link 'My Cart' in the top.links with a class name on the <li> and <a> tags however the link no longer displays the current count in the user's cart, for example: My Cart (4 items).

How can a class name be added to the <li> and <a> tags for the My Cart link AND continue to display the current cart count (without any core overrides to the addCartLink method) ?

A basic example of the goal output:

<li class="my-cart-list-item">
  <a href="/checkout/cart" class="my-cart-anchor">My Cart(4 items)</a>
</li>

Edit: This question is for default Magento CE 1.9, not Magento CE 1.9 RWD theme.

1

I spent time working through a 'solution' for this a few weeks after posting the question. I do not think the problem should be solved in this manner as the best solution would be for the addCartLink method to accept <li> and <a> parameter updates like the other links in top.links. If this could be updated via XML then I think it would be considered a more Magento-like solution, however updating via XML does not seem to be built in to Magento at this time.

My proposed solution -

https://gist.github.com/jasontipton/80be864a3eefacdc65b0

The important part of this is Gist for this question is:

            case strpos($pageLink,'/checkout/cart/') !== false:
                $activeID = "top-cart";
                if (strstr($pageUri,'/checkout/cart/')) {
                    $activeClass = true;
                }                    
                break;

It checks the page URL and if matches /checkout/cart/ then it adds a class. Again, if there is a better solution I would prefer to use that and am completely open to suggestions.

1

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


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

0

The standard top links in Magento use the template file template/page/template/links.phtml which is defined as part of the block Mage_Page_Block_Template_Links so I see you have two options either simply create your own version of this file inside your site's theme or you can set the template to your own template via some layout xml.

Either way you will simply then be able to work with your new template file and add what ever you what in regards for html to these links and list items.

  • Unfortunately links.phtml is also used for building links in the footer so editing it can effect a column of links there too. – Jason Tipton Jan 22 '15 at 17:12
  • @JasonTipton then set it via the layout xml and so only change the one instance you need – David Manners Jan 22 '15 at 17:19
  • are you meaning to update layout xml so that the top.links calls its own copy of links.phtml (let's call it top-links.phtml) and then make the edits there? – Jason Tipton Jan 22 '15 at 17:30
  • @JasonTipton indeed that sounds good – David Manners Jan 22 '15 at 17:31
  • I'll post how I ended up solving this (it doesn't involve duplicating the links.phtml file though) and see what you think of it. – Jason Tipton Jan 22 '15 at 17:36
0

The functionality you want in clear Magento is stored in the following file (for CE): app\design\frontend\rwd\default\template\checkout\cart\minicart.phtml it's not just mere template link.

Watch app\design\frontend\rwd\default\layout\checkout.xml for reference:

<block type="checkout/cart_minicart" name="minicart_head" 
template="checkout/cart/minicart.phtml" before="-">
     ...
</block>

In this case you will copy this file: app\design\frontend\rwd\default\template\checkout\cart\minicart.phtml into your theme.

Enable your Template Path Hints and look what template your site's using. If output of this block was removed from your site, you should turn it on in your local.xml:

<default>
    <reference name="header">
        <--- your block here --->
  • You are providing enterprise paths for a CE edition. – SR_Magento Jan 22 '15 at 15:00
  • Thank you @La Mi I checked and checkout/cart/minicart.phtml does exist for Magento CE 1.9 RWD theme. I meant the question to be for default Magento CE 1.9, not the RWD theme. Default Magento CE 1.9 does not include checkout/cart/minicart.phtml so this does solve the question. I will add a note to the question to clarify this. – Jason Tipton Jan 22 '15 at 17:03
  • Clean CE 1.9.1.0 version from magentocommerce.com does have rwd theme (it's provided for presentation purposes). You can copy checkout/cart/minicart.phtml from it to your current theme. You are familiar with Magento's theme fallback, I guess? If no, you should get familiar with: (magentocommerce.com/knowledge-base/entry/…) You just need this code from rwd theme to move to your theme and then you can modify everyting you need. – Amberta Jan 22 '15 at 18:39
  • Btw, you wrote: "however the link no longer displays the current count in the user's cart". That means that your theme also had this file. You just removed it from output. You should have enabled Path hints in your store or look what template "checkout_cart_link" was including. Then you should have changed the file to add some class to link. You are doing your task wrong. – Amberta Jan 23 '15 at 8:49

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.