Magento Sidebar Featured Products

This tutorial will explain how to add a featured product to the sidebar. In this example we will add the featured product to the right sidebar. The steps we will follow are as follows.

  1. Create a new category in the magento admin area to contain our featured products.
  2. Add the block calls to the XML Layout
  3. Create the phtml template files to be referenced by the block

Category

Add a new category to your store to contain your featured products. Make it inactive and jot down the id that it is given once saved. Add any products you want to consider featured to this category. Also make sure this category is not a root category.

Layout

Open page.xml and inside your default handle’s right block add the following line.

<block type="catalog/navigation" name="featured" template="catalog/featured_random.phtml" />

My finished right block looks as follows:

<block type="core/text_list" name="right" as="right">
	<block type="catalog/navigation" name="featured" template="catalog/featured_random.phtml" />
	<block type="catalog/navigation" name="category.listing" template="catalog/navigation/categories.phtml" />
</block>

Template

Use the following code for featured_random.phtml

<?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 license@magentocommerce.com 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_default
* @package    Mage
* @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/
?>
 
<?php 
$category_id = "25"; // category_id for "Featured Products"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
  ->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
  ->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
 
<?php if($_productCollection->count()): ?>
 
  <?php 
  $products = array();
  foreach ($_productCollection as $_product) { array_push($products, $_product); }
  $_product = $products[rand(0,count($products)-1)];
  ?>
<div class="block block-featured-product">
  <div class="block-title">
    <h2>Featured Product</h2>
  </div>
  <div class="block-content">
    <ul id="featured-product-list">
      <li class="featured-product">
          <h4><?php echo $this->htmlEscape($_product->getName())?></h4>
          <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
            <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(117, 117); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
          </a>
          <a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">View Item</a>
      </li>
    </ul>
  </div>
</div>
<?php endif; ?>

Resources:

19 thoughts on “Magento Sidebar Featured Products

  1. Magento Featured Products in Sidebar « Brian Wigginton | Drakz Free Online Service

  2. Thanks! This is great. I would like to add several featured products on the same page. My solution has been to just copy and paste the above code in the same .phtml file. It works, but I often end up with the same featured product being displayed. Is there a better way? Or a way to ensure different featured products will appear? If so, any advice would be appreciated. Thanks agian.

    • @Andy, I think what you would want to do is remove the product (something like unset($_product[YOUR_RANDOM_ARRAY_INDEX]) ) from the $_product array after you’ve added it to your featured product list. That way you can prevent duplicates from being on the same page. Hope this helps, if you haven’t already figured it out :)

  3. This is a great bit of code – thanks for taking the time. I have a featured product promotion working for my homepage (I disabled the random aspect). However, I would like to show multiple products from the same category but can’t work out how to tweak the code to make this happen. Is there an easy fix? Thanks once again for the above code.
    I

  4. Worked it out. It’s basically a tweak of the above code.

    Inserted a ‘foreach’ call after the beginning of the unordered list and ended it directly after the end.

    I now have all items in a ‘featured’ category listed in the sidebar (currently 5 items but can be more or less).

    addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
    ?>

    count()): ?>

    Featured Products

    <a href="getProductUrl() ?>" title="htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">htmlEscape($_product->getName())?>
    <span class="view-item-button" href="getProductUrl() ?>" title="htmlEscape($_product->getName()) ?>">View Item

    <a href="getProductUrl() ?>" title="htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
    <img class="product-image" src="helper('catalog/image')->init($_product, 'small_image')->resize(50, 50); ?>" alt="htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />

  5. Thanks for the code ! I used this to create a random product display in the sidebar. Works really nice, just needed some tiny changes to make it work in a multiple language website.

  6. This artical is very useful,I have test the module,it is good.But I have an issue,I find there is only one product that display in the frontend,and in the code I can’t set the number of products that I want to display.I am a new magenter,how should I do?Thank you!

    • This is very helpful. Thank you. I’m finding it works for adding just one featured product.

      I am using Magento 1.7.

      I added:

      to catalog.xml around line 60 between tags.

      And then I saved the provided template code in a file located in: catalog/product/random.phtml and it works perfectly.

      To get more featured products displaying in my right sidebar I copied:

      and pasted it twice into catalog.xml. (I’m not sure if this is a good fix in the long run, I’ll have to see how it goes.)

  7. Hi there,

    Ive got this code working in magento 1.7.2, but i cant seem to get more then 1 product to show, I have copied and pasted the element and get more then 1 product displaying, but they are often duplicate products..

    I tried using iain’s code above ( atleast tried to piece it together ) but it seems its incomplete.

    Any help would be great

    Regards

    - Andy

  8. Brilliant! Just what is needed only I also only have one product showing? Previous comment/answers above seem to have code snippets missing. Thanks in advance for any help!

  9. hi,
    i am using this, now i have featured products on my sidebar. but it displays only one product at a time, i need to display 3 products at a time.

    please reply…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>