Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Display Cart Subtotal In Header? 
 
leetaylordoes
Sr. Member
 
Total Posts:  227
Joined:  2008-02-05
San Diego, CA
 

Hi All,

I’m researching to see which files need to be modified in order to display the cart subtotal and number of items in the cart inside of the header.

Anyone know of a quick reference while I search through?

I know the credentials get displayed via “/template/checkout/cart/sidebar.phtml” natively.
I need do display it via “/template/page/html/header.phtml”.

Thanks,
Lee

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

app/code/core/mage/checkout/block/cart/abstract.php will show you the code used to get the cart total.

app/design/*/*/Template/checkout/total.phtml also shows you the use in getting the total.

now...for this problem...template path hints show this .phtml file with it’s block

frontend/default/default/template/page/template/links.phtml
Mage_Page_Block_Template_Links

This block / links.phtml are resued in the footer. If you check out that php block, you will see that it simply has an array and a function to add links to it—The links added are not hard coded anywhere in a phtml file or a php file! This tells us that you will find the links added via an XML file (magento reads xml so it’s capable of using code rasberry ).
Starting at page.xml you find:

<block type="page/html_header" name="header" as="header">
                <
block type="page/template_links" name="top.links" as="topLinks"/>
                <
block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
                <
block type="core/text_list" name="top.menu" as="topMenu"/>
            </
block>

Uh oh, this isn’t as helpful :(
page/template_links means go to app/code/core/mage/page/template/link.php and app/design/*/*/page/template/toplinks.phtml (or something like that) Thse appeared to be dry holes for me.
OK, next, check out the Core section (core/text_list)
Core only has “messages.phtml” here. A dry hole. The code section has a lot, but like the page section, it’s all geared towards adding links, not actually hard coding the links that go up there. So, we have to keep looking.

Well, what is this “checkout” and “cart” button related to? Well the CART of course!
Check out cart.xml....

//on the top of the cart !!
    <default>

        <!-- 
Mage_Checkout -->
        <
reference name="top.links">
            <
block type="checkout/links" name="checkout_cart_link">
                <
action method="addCartLink"></action>
                <
action method="addCheckoutLink"></action>
            </
block>
        </
reference>

Finally, getting somewhere! Uh oh, no hard coded links here (this is actually a good thing). What you DO see is two methods - addCartLink and addCheckoutLink!! Awesome, we are close to finding where these links are created....

Where are these methods? Well the block is “checkout/links” so we have to go digging in the core code ...app/code/core/Mage/Checkout/Block/Link.php:

public function addCartLink()
    
{
        
if ($parentBlock $this->getParentBlock()) {
            $count 
$this->helper('checkout/cart')->getSummaryCount();

            if( 
$count == {
                $text 
$this->__('My Cart (%s item)'$count);
            
elseif( $count {
                $text 
$this->__('My Cart (%s items)'$count);
            
else {
                $text 
$this->__('My Cart');
            
}

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

    
public function addCheckoutLink()
    
{
        
if (!$this->helper('checkout')->canOnepageCheckout()) {
            
return $this;
        
}
        
if ($parentBlock $this->getParentBlock()) {
            $text 
$this->__('Checkout');
            
$parentBlock->addLink($text'checkout'$texttrue, array(), 60null'class="top-link-checkout"');
        
}
        
return $this;
    
}
}

Here is where to add your code!!
Grab some code from abstract.php (first file I told you about above) and play around with it to get the cart total as part of the $text in your Cart link! Shouldn’t be too hard.

I hope you don’t find my writing style in this post to be condescending (totally not my intent). I kinda just started writing as a narrative...idk why.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

heh, after re-reading your post, it looks like I totally made up a solution to a different problem..anyway, my stuff above is geared towards writing an order total as part of the links in the header area (cart link)....

I totally missed the mark...(but learned from it! - hope you do too!)

anyway, on to find your solution...will edit soon…

edit: looks like i already answered this one:  http://www.magentocommerce.com/boards/viewthread/13046/
rasberry

 
Magento Community Magento Community
Magento Community
Magento Community
 
leetaylordoes
Sr. Member
 
Total Posts:  227
Joined:  2008-02-05
San Diego, CA
 

@nikefido,

heh, no worries on the narrative. I’m looking through the files along with you and will report if I find something useful as a solution.

I appreciate your second pair of eyes looking at this.

- Lee

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

see my edit :D

That displays the entire mini-cart in the header. More likely you can just use some code from it to get the subtotal etc (esp. see that abstract class above - you should be able to use those functions by extending that class or re-writing them into a block...don’t forget to not edit core code - better to overwrite specific classes so you can update Magento without your code breaking.....maybe )

 
Magento Community Magento Community
Magento Community
Magento Community
 
leetaylordoes
Sr. Member
 
Total Posts:  227
Joined:  2008-02-05
San Diego, CA
 

I see smile

Good work, as it helped speed up my progress.

Thanks again,
Lee

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mike
Member
 
Total Posts:  43
Joined:  2007-09-11
Los Angeles, CA
 

Even though this is an old thread, I thought I’d add this note…

The solution to displaying a cart’s subtotal in a Magento site’s header has been well-documented in this post on another thread:
http://www.magentocommerce.com/boards/viewthread/26443/#t92199

Hopefully this will save some time for anyone else seeking a similar solution!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top