Magento - add sidebar mini cart on the header

Posted by Damodar Bashyal on October 05, 2011

 

Do you want to move magento's sidebar mini cart to header? If you said "yes' then follow the following post. We are just using exisitng sidebar mini cart and modified to display on the top header. We have removed some of the Magento's code from the cart which we didn't need in our case, if you need it you can re-include from sidebar's phtml file that comes with magento e-commerce's base template.

Add below code inside <default> on checkout.xml:

app\design\frontend\your-package\your-template\layout\checkout.xml

<reference name="header">
	<block type="checkout/cart_sidebar" name="cart_cartheader" template="checkout/cart/cartheader.phtml" before="-">
    	<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/cartheader/default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/cartheader/default.phtml</template></action>
        <action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/cartheader/default.phtml</template></action>

        <block type="core/text_list" name="cart_cartheader.extra_actions" as="extra_actions" translate="label" module="checkout">
        	<label>Shopping Cart Cartheader Extra Actions</label>
    	</block>
	</block>
</reference>

Create new file cartheader.phtml under checkout/cart as below and add the below code in it:

app\design\frontend\your-package\your-template\template\checkout\cart\cartheader.phtml

<div class="mini-cart-layer">
	<?php if ($this->getIsNeedToDisplaySideBar()):?>
		<div class="top-cart">
			<?php $_cartQty = $this->getSummaryCount() ?>
			<?php if ($_cartQty > 0): ?>
				<?php $_myCart = $this->__('Shopping Cart (%s items)', $_cartQty) ?>
			<?php else: ?>
				<?php $_myCart = $this->__('Shopping Cart (0 item)') ?>
			<?php endif ?>
			
			<?php if ($this->getIsLinkMode() || !$this->getIsNeedToDisplaySideBar()):?>
				<div class="block-title no-items">
					<ul class="links cart-link">
						<li ><a href="<?php echo $this->getUrl('checkout/cart'); ?>" rel="nofollow"><?php echo $_myCart ?></a></li>
					</ul>
				</div>
			<?php else:?>
				<div class="block-title<?php if(!$_cartQty) { echo (' no-items'); } ?>">
					<span id="cartHeader"><?php echo $_myCart ?></span>
				</div>
				<div id="topCartContent" class="block-content" style="display:none">
					<div class="inner-wrapper"><?php /*extra div to smooth slideUp and slideDown*/ ?>
						<?php $_items = $this->getRecentItems() ?>
						<?php if(count($_items)): ?>
							<p class="block-subtitle">
								<span onclick="toggleTopCart();" class="close-btn"><?php echo $this->__('Close'); ?></span>
								<?php echo $this->__('Recently added item(s)') ?>
							</p>
							<ol id="mini-cart" class="mini-products-list">
								<?php foreach($_items as $_item): ?>
									<?php echo $this->getItemHtml($_item) ?>
								<?php endforeach; ?>
							</ol>
							<script type="text/javascript">decorateList('mini-cart', 'none-recursive')</script>
						<?php else: ?>
							<p class="block-subtitle">
								<span onclick="toggleTopCart()" class="close-btn"><?php echo $this->__('Close'); ?></span>
									<?php echo $this->__('Recently added item(s)') ?>
							</p>
							<p class="cart-empty">
								<?php echo $this->__('You have no items in your shopping cart.') ?>
							</p>
						<?php endif ?>
						<?php if($_cartQty && $this->isPossibleOnepageCheckout()): ?>
							<div class="actions">
								<a href="<?php echo $this->getUrl('checkout/cart'); ?>" rel="nofollow"><?php echo $this->__('Go to Shopping Cart') ?></a>
								<button class="button" type="button" onclick="setLocation('<?php echo $this->getCheckoutUrl() ?>')"><span><span><?php echo $this->__('Checkout') ?></span></span></button>
							</div>
						<?php endif ?>
					</div>
				</div>
			<?php endif;?>
		</div>
	<?php endif;?>
</div>

Now create new file default.phtml under checkout/cart/cartheader as below and add the following code:

app\design\frontend\your-package\your-template\template\checkout\cart\cartheader\default.phtml

<?php
$_item = $this->getItem();
$isVisibleProduct = $_item->getProduct()->isVisibleInSiteVisibility();
//$canApplyMsrp = Mage::helper('catalog')->canApplyMsrp($_item->getProduct(), Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_BEFORE_ORDER_CONFIRM);
?>
<li class="item">
	<?php if ($this->hasProductUrl()): ?>
		<a href="<?php echo $this->getProductUrl()?>" title="<?php echo $this->htmlEscape($this->getProductName()) ?>" class="product-image"><img src="<?php echo $this->getProductThumbnail()->resize(50, 50)->setWatermarkSize('30x10'); ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($this->getProductName()) ?>" /></a>
	<?php else: ?>
		<span class="product-image"><img src="<?php echo $this->getProductThumbnail()->resize(50, 50)->setWatermarkSize('30x10'); ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($this->getProductName()) ?>" /></span>
	<?php endif; ?>
	<div class="product-details">
		<p class="product-name">
			<?php if ($this->hasProductUrl()): ?>
				<a href="<?php echo $this->getProductUrl() ?>">
			<?php endif; ?>
				<?php echo $this->htmlEscape($this->getProductName()) ?>
			<?php if ($this->hasProductUrl()): ?>
				</a>
			<?php endif; ?>
		</p>
		<a href="<?php echo $this->getDeleteUrl() ?>" title="<?php echo $this->__('Remove This Item') ?>" onclick="return confirm('<?php echo $this->__('Are you sure you would like to remove this item from the shopping cart?') ?>');" class="top-btn-remove">
			<?php echo $this->__('Remove This Item') ?>
		</a>
	</div>
</li>
//on the page/html/header.phtml, add below
<?php echo $this->getChildHtml('cart_cartheader') ?>

Now we need Javascript to slide mini cart up and down (slide toggle). To achieve that toggle effect add the following code on new javascript file named cf.js

skin\frontend\your-package\your-template\js\cf.js

/**
 * Author: damodar.bashyal
 * Date: 5/10/11
 * Time: 12:26 PM
 */
function slideUp()
{
	jQuery('#topCartContent:visible').slideUp(1000);
	jQuery('.mini-cart-layer').addClass('mini-cart-layer-up');
	jQuery('.mini-cart-layer').removeClass('mini-cart-layer-down');
}

function slideDown()
{
	jQuery('#topCartContent:hidden').slideDown(1000);
	/*startTimer()*/ /* optional*/
	jQuery('.mini-cart-layer').addClass('mini-cart-layer-down');
	jQuery('.mini-cart-layer').removeClass('mini-cart-layer-up');
}

function toggleTopCart()
{
	if(jQuery('#topCartContent').is(':visible'))
	{
		slideUp();
	} else {
		slideDown();
	}
}

var timer;
function startTimer()
{
	timer = setTimeout(function(){
		slideUp();
	}, 5000);
}

jQuery(document).ready(function(){
	jQuery('.mini-cart-layer .top-cart .block-title #cartHeader').click(function(){
		toggleTopCart();
	});

	jQuery('.mini-cart-layer .top-cart .block-title #cartHeader').mouseover(function(){
		clearTimeout(timer);
	}).mouseout(function(){
		startTimer();
	});

	jQuery("#topCartContent").mouseover(function() {
		clearTimeout(timer);
	}).mouseout(function(){
		startTimer();
	});
});

To include above file on the <head></head> section add the below line on page.xml after jquery inclusion.

app\design\frontend\your-package\your-template\layout\page.xml

<action method="addItem"><type>skin_js</type><name>js/jquery.js</name><params/></action>
<action method="addItem"><type>skin_js</type><name>js/cf.js</name><params/></action>

Now time for CSS for the tabs: You need to modify as your need and replace images.

/*---*/
.clear:after {
    clear: both;
    content: ".";
    display: block;
    height: 0;
    visibility: hidden;
}
div.mini-cart-layer {
    background: url("../images/view_shopping_cart.jpg") no-repeat scroll 0 0 transparent;
    height: 26px;
    padding: 0;
    position: absolute;
    right: 0;
    top: 9px;
    width: 165px;
    z-index: 99;
}
.top-cart .top-btn-remove {
    background: url("../images/btn_remove.gif") no-repeat scroll 0 0 transparent;
    display: block;
    font-size: 11px;
    height: 15px;
    line-height: 11px;
    margin: 0 0 3px;
    overflow: hidden;
    padding: 0 0 0 15px;
}
div.mini-cart-layer-up{}
div.mini-cart-layer-down{background-position: 0 bottom;}
.mini-cart-layer span#cartHeader {
    display: block;
    height: 26px;
    overflow: hidden;
    text-align: left;
    text-indent: -99999px;
    width: 165px;
}
.top-cart {
    float: left;
    position: relative;
}
.top-cart .block-title {
    white-space: nowrap;
    cursor: pointer;
}
.top-cart .block-title.expanded {
    background-position: 0 3px !important;
}
.top-cart .block-title.expanded span {
    background-position: 100% -126px !important;
}
.top-cart .block-content {
    background: none repeat scroll 0 0 #FDFDFD;
    font-size: 11px;
    position: absolute;
    right: 0;
    text-align: left;
    top: 20px;
    width: 267px;
}
.top-cart .block-content ol {
    margin: 0;
    padding: 0;
    list-style: none outside none;
}
.top-cart .inner-wrapper {
    border: 4px solid #DB4C6A;
}
.top-cart .block-content .block-subtitle {
    background: #DB4C6A;
    font-size: 10px;
    font-weight: bold;
    color: #fff;
    line-height: 12px;
    padding: 3px 10px 4px;
}
.top-cart .block-content .block-subtitle .close-btn {
    float: right;
    width: 13px;
    height: 0;
    padding-top: 12px;
    text-align: left;
    overflow: hidden;
    cursor: pointer;
    background: url(../images/btn_remove.gif) no-repeat 0 0;
    position: relative;
    z-index: 1;
}
.top-cart .cart-empty .close-btn {
    float: right;
    width: 13px;
    height: 0;
    padding-top: 12px;
    text-align: left;
    overflow: hidden;
    cursor: pointer;
    background: url(../images/btn_remove.gif) no-repeat 0 0;
    position: relative;
    z-index: 1;
}
.top-cart .cart-empty {
    padding: 10px 10px 10px 20px;
    color: #666;
}
.top-cart .block-content .item {
    padding: 8px 5px 8px 11px;
    border-bottom: 1px solid #DDD;
    margin: 0 5px;
}
.top-cart .block-content .last {
    border-bottom: none;
}
.top-cart .block-content .item .product-name {
    font-size: 11px;
    height: 30px;
    line-height: 14px;
    margin: 0 0 5px;
    overflow: hidden;
}
.top-cart .block-content .item .product-name a {
    font-size: 11px;
    text-decoration: none;
    color: #444;
}
.top-cart .block-content table {
    margin: 5px 0 0 0;
}
.top-cart .block-content table th {
    padding: 1px 8px;
    color: #8f8f8f;
    text-align: right;
}
.top-cart .block-content table td {
    text-align: left;
    padding: 1px 0;
}
.top-cart .block-content .subtotal {
    padding: 2px 5px;
    text-align: center;
    color: #666;
}
.top-cart .actions {
    background-color: #DB4C6A;
    color: #FFFFFF;
    padding: 5px 11px;
    text-align: right;
    z-index: 999;
}
.top-cart .actions a {
    float: left;
    line-height: 23px;
}
.top-cart .actions button {
    float: none;
    color: #FFF;
    padding-top: 3px;
}
div.top-cart {
    float: none;
}
div.top-cart .block-title {
    color: #000000;
    display: block;
    float: none;
    font-family: arial;
    font-size: 12px;
    font-weight: bold;
    padding: 0;
}
div.top-cart .block-content {
    top: 26px;
}
div.top-cart .block-content ol li {
    display: block;
    float: none;
}
/*===*/
Note: As this header mini-cart depends on sidebar mini cart, make sure it's active at:
magento admin / system / configuration / Sales / Checkout / Shopping Cart Sidebar
Select "YES" for Display Shopping Cart Sidebar.

simon posted on - Monday 19th of December 2011 05:33:09 AM

Hi, thank you very much for the detailed instructions. Great! Just 2 things: on your checkout.xml code, there is on line 3 a closing tag ">" missing on an action.

Regarding the cf.js file, you say to insert a line on the on page.xml after jquery inclusion. What exactly do you mean with "after jquery inclusion"? I only have the prototype js in my magento.
On the other side, no matter where I insert the line in page.xml it wont work...

Thank you

damodar bashyal posted on - Monday 19th of December 2011 05:40:19 AM

You need Jquery as well for above js to work. thats why i asked to include jquery as well.

Damodar Bashyal posted on - Friday 27th of April 2012 11:55:21 AM

@umain not sure what you mean. It should work with any latest jQuery. Are you getting any specific error?

If I know what error you all are getting, I can look into that but if you say I am getting error, it's hard to tell the solution.

umain posted on - Friday 27th of April 2012 11:37:32 AM

which jquery has to include,i have done the same as you describe above,but it is not working in my site

Mrugen posted on - Friday 20th of January 2012 02:23:44 PM

i have done the same thing as you describe.
but it not working in my site.

is that add to cart process same as in this site?
http://www.vistastores.com/

Damodar Bashyal posted on - Sunday 11th of March 2012 10:39:14 AM

Whats the issue exactly? More information would have helped to solve it.

Tomas Berglund posted on - Wednesday 2nd of May 2012 09:53:24 PM

Can you make this cart implementation on our site ?
And if so to what cost

Damodar Bashyal posted on - Wednesday 2nd of May 2012 09:57:00 PM

Hi Tomas,
Yes i can. I have sent you quote in your email. one of the sites i had applied this before is: http://www.binglee.com.au/home-appliances/ovens-ranges/

Damodar Bashyal posted on - Tuesday 8th of May 2012 01:25:45 PM

Today when I was moving sidebar cart to header, I found out why above code was not working because I deleted closing lt from action. It was </action instead of </action>.

Dykwia posted on - Saturday 26th of May 2012 10:44:57 PM

Hi,

Thanks for the tut, just what we need. Sort of;-)
We do not see the recently added items in the scroll down, there's is the header "recently added...". But nothing shows. Any idea?

Thanks,
Paul.

Damu posted on - Saturday 26th of May 2012 11:01:19 PM

@Dykwia it seems everything working except item lists.
So compare two phtml files with the latest magento version you have in 'base' folder.
If you can't fix it, you can send me FTP and I can check it for you. But try to compare files first as they could be different in new magento versions.

Slaine posted on - Thursday 7th of June 2012 10:59:58 AM

Hello,
thank you for this great tutorial :)
Magento 1.6 folds of the cart, unfortunately, not when an item is added. did you have a solution?
regards

Damodar Bashyal posted on - Thursday 7th of June 2012 11:03:52 AM

@Slaine not sure what you meant.

Norbert Hoese posted on - Wednesday 25th of July 2012 11:59:09 AM

Hello,

Can you make this cart implementation on our site ?
And if so to what cost? I want design like here:
http://www.shopwaredemo.de/

Damodar Bashyal posted on - Wednesday 25th of July 2012 12:21:13 PM

@Norbert Sorry, currently i have too many projects on hand for fast turnaround and I am not exactly sure what you wanted from that website. I charge $90/hr with minimum 2hr job.

Magento Developer posted on - Wednesday 1st of August 2012 10:26:50 AM

@Norbert: We can do this for you. Contact us at [email protected] with details.

Thanks
EMD

DAVID HAWKINS posted on - Thursday 23rd of August 2012 09:50:04 PM

hey there, used this for about a week, worked great and then suddenly, i came back to my desk and it had gone, disappeared from my site completely, please if anyone knows why this might have happened let me know i would really really appreciate it

Cai posted on - Wednesday 3rd of October 2012 11:23:40 AM

@Damodar

Great add on! Thanks!

I have a very small problem with it.

On this demo site you mention:

http://www.binglee.com.au/home-appliances/ovens-ranges/

When you add to cart, the cart roll out comes out automatically?

On my site http://www.motamec.com/

it doesnt do that. How do I make it do that?

Thanks

Danko posted on - Wednesday 31st of October 2012 11:47:06 AM

Hello,

First off love the tuto,
I would like to now if there is a easy way not to add a btn but to use the one that is already in top links

Hope some can help

Greetings Danko

Puneet posted on - Friday 29th of March 2013 07:38:12 AM

Hi
I have other problem related to Cart Sidebar. When I add an option for configurable product to shopping cart, it shows both the parent product image and associated product image in sidebar. How can we handle this.

ROHIT posted on - Monday 8th of July 2013 06:42:03 AM

I HAVE TRY THIS CODE BUT THERE IS NO ACTION ON ANY PAGE NO DROP DOWN IS SHOWN OR ANY CART IS SHOWN EVEN IF LINK IS NOT SHOWN

Magento Module Ajax Cart posted on - Thursday 27th of June 2013 10:37:17 PM

Adding cart to sidebar is very useful according to end user point of view and this will help to improve the conversion. Really nice info provided here.

vishal posted on - Wednesday 11th of December 2013 07:40:57 AM

hello why you use new file default.phtml under checkout/cart/cartheader

Tom posted on - Tuesday 20th of May 2014 07:21:54 AM

HI mat,e thanks for the work. I only seem to be getting 1 product in the cart which is a bundle product. the count is returning 3 which the limit is set to for the sidecart in the config. Any ideas why?
Thanks

Harold posted on - Friday 21st of November 2014 12:53:59 AM

Hello, i have install your app, but i only get the text, 1 item in shopping cart, can you please say me what im doing wrong? site: www.bxparts.eu
 
not published on website


QR Code: Magento - add sidebar mini cart on the header