Menu
Web Design | SEO | Website Development

UK Based Web Agency, Magento & Wordpress Specialist (Cranleigh | Surrey)

Hiding Empty Widgets in Magento

By In Platforms, Website Development On 08/03/2012


This is a nifty little hack I found for Magento which enable you to hide your empty cart and compare product widgets, as in my opinion they are irrelevant if empty and take up precious page real estate.

Compare widget:

  • Get your sidebar.phtml from:
    public_html/app/design/frontend/base/default/template/catalog/product/compare/
  • Add this to the top:
<?php if($this->helper('catalog/product_compare')->getItemCount()>0): ?>
  • Add this to the bottom:
<?php endif; ?>
  • so that you end up with something like this:
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
/* @var $this Mage_Catalog_Block_Product_Compare_Sidebar */
?>
<?php if($this->helper('catalog/product_compare')->getItemCount()>0): ?>
<?php
$_helper = $this->helper('catalog/product_compare');
$_items = $_helper->getItemCount() > 0 ? $_helper->getItemCollection() : null;
?>
<div class="block block-list block-compare">
<div class="block-title">
<strong><span><?php echo $this->__('Compare Products') ?>
<?php if($_helper->getItemCount() > 0): ?>
<small><?php echo $this->__('(%d)', $_helper->getItemCount()) ?></small>
<?php endif; ?>
</span></strong>
</div>
<div class="block-content">
<?php if($_helper->getItemCount() > 0): ?>
<ol id="compare-items">
<?php foreach($_items as $_index => $_item): ?>
<li class="item">
<input type="hidden" class="compare-item-id" value="<?php echo $_item->getId() ?>" />
<a href="<?php echo $_helper->getRemoveUrl($_item) ?>" title="<?php echo $this->__('Remove This Item') ?>" class="btn-remove" onclick="return confirm('<?php echo $this->__('Are you sure you would like to remove this item from the compare products?') ?>');"><?php echo $this->__('Remove This Item') ?></a>
<p class="product-name"><a href="<?php echo $this->getProductUrl($_item) ?>"><?php echo $this->helper('catalog/output')->productAttribute($_item, $_item->getName(), 'name') ?></a></p>
</li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('compare-items')</script>
<div class="actions">
<a href="<?php echo $_helper->getClearListUrl() ?>" onclick="return confirm('<?php echo $this->__('Are you sure you would like to remove all products from your comparison?') ?>');"><?php echo $this->__('Clear All') ?></a>
<button type="button" title="<?php echo $this->__('Compare') ?>" class="button" onclick="popWin('<?php echo $_helper->getListUrl() ?>','compare','top:0,left:0,width=820,height=600,resizable=yes,scrollbars=yes')"><span><span><?php echo $this->__('Compare') ?></span></span></button>
</div>
<?php else: ?>
<p class="empty"><?php echo $this->__('You have no items to compare.') ?></p>
<?php endif; ?>
</div>
</div>
<?php endif; ?>

Cart Widget:

  • Get your sidebar.phtml from:
    public_html/app/design/frontend/base/default/template/checkout/cart/
  • Add this to the top:
<?php $_cartQty = $this->getSummaryCount() ?>
<?php if ($_cartQty>0): ?>
  • Add this to the bottom:
<?php endif;?>
  • so that you end up with something like this:
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/

/**
* Shoping cart sidebar
*
* @see Mage_Checkout_Block_Cart_Sidebar
*/
?>
<?php $_cartQty = $this->getSummaryCount() ?>
<?php if ($_cartQty>0): ?>
<?php if ($this->getIsNeedToDisplaySideBar()):?>
<div class="block block-cart">
<?php $_cartQty = $this->getSummaryCount() ?>
<div class="block-title">
<strong><span><?php echo $this->__('My Cart') ?></span></strong>
</div>
<div class="block-content">
<?php if ($_cartQty>0): ?>
<div class="summary">
<?php if ($_cartQty==1): ?>
<p class="amount"><?php echo $this->__('There is <a href="%s">1 item</a> in your cart.', $this->getUrl('checkout/cart')) ?></p>
<?php else: ?>
<p class="amount"><?php echo $this->__('There are <a href="%s">%s items</a> in your cart.', $this->getUrl('checkout/cart'), $_cartQty) ?></p>
<?php endif ?>
<p class="subtotal">
<?php if ($this->canApplyMsrp()): ?>
<span class="map-cart-sidebar-total"><?php echo $this->__('ORDER TOTAL WILL BE DISPLAYED BEFORE YOU SUBMIT THE ORDER'); ?></span>
<?php else: ?>
<span class="label"><?php echo $this->__('Cart Subtotal:') ?></span> <?php echo Mage::helper('checkout')->formatPrice($this->getSubtotal()) ?>
<?php if ($_subtotalInclTax = $this->getSubtotalInclTax()): ?>
<br />(<?php echo Mage::helper('checkout')->formatPrice($_subtotalInclTax) ?> <?php echo Mage::helper('tax')->getIncExcText(true) ?>)
<?php endif; ?>
<?php endif; ?>
</p>
</div>
<?php endif ?>
<?php if($_cartQty && $this->isPossibleOnepageCheckout()): ?>
<div class="actions">
<?php echo $this->getChildHtml('extra_actions') ?>
<button type="button" title="<?php echo $this->__('Checkout') ?>" class="button" onclick="setLocation('<?php echo $this->getCheckoutUrl() ?>')"><span><span><?php echo $this->__('Checkout') ?></span></span></button>
</div>
<?php endif ?>
<?php $_items = $this->getRecentItems() ?>
<?php if(count($_items)): ?>
<p class="block-subtitle"><?php echo $this->__('Recently added item(s)') ?></p>
<ol id="cart-sidebar" class="mini-products-list">
<?php foreach($_items as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('cart-sidebar', 'none-recursive')</script>
<?php else: ?>
<p class="empty"><?php echo $this->__('You have no items in your cart.') ?></p>
<?php endif ?>
</div>
</div>
<?php endif;?>
<?php endif;?>

Works in Magento ver. 1.6.2.0…


About the Author

Dan Vince

Yet another Web Development Professional

Related Posts

3 Comments

  • Brian 5 YEARS AGO

    excellent!

    Reply
  • Peter 3 YEARS AGO

    Very handy bit of code thanks. Should work like this out the box really.

    Reply
  • Guest 3 YEARS AGO

    Hello, this helped me. However, the "Checkout" button still appears on sidebar. How can I hide it when cart is empty? Thanks.

    Reply

Leave a comment