6 Steps to creating a featured products jQuery slider for Magento

65
6 Steps to creating a featured products jQuery slider for Magento

This tutorial has come out of necessity for a homepage featured product slider, I had tried quite a few different tutorials but never achieved what I was looking for. Magento is a great open-source ecommerce package, but Im a little surprised that featured products isn’t a feature built-in. Hopefully this tutorial will help. Help support open source!

3graces.com Demo

Requirements:

Step 1: Create a Feature Products Category.
Make sure the category is directly under your root category, or this will not work.
(see screenshot). Take note of the category ID, you will need this later.

3graces category screenshot

Step 2: Create a feature products block
You will need to create featured-products.phtml and place it in /app/design/frontend/yourtheme/template/catalog/product/

Use the following code in featured-products.phtml:
[php]
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-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/osl-3.0.php Open Software License (OSL 3.0)
*/
?>
<?php
/**
* Product list template
*
* @see Mage_Catalog_Block_Product_List
*/
?>

<style>
.featured-bg {
background: url(/skin/frontend/3graces/default/images/hp_featured_product_bg.gif);
width: 146px;
height: 144px;
}

.featured-bg div {
position: relative;
top: 10px;
left: 20px;
}

.featured-bg div img {
width: 100px;
}

#slider ul, #slider li {
margin: 0;
padding: 0;
list-style: none;
}

#slider li {
width: 470px;
height: 175px;
overflow: hidden;
}

span#prevBtn {

}

span#nextBtn {

}

/* numeric controls */

ol#controls {
margin: .1em 0;
padding: 0 0 5px 0;
height: 20px;
}

ol#controls li {
margin: 0 5px 0 0;
padding: 0;
float: left;
list-style: none;
height: 20px;
line-height: 20px;
}

ol#controls li a {
float: left;
height: 20px;
line-height: 20px;
border: 1px solid #ccc;
background: #EDE4E7;
color: #555;
padding: 0 5px;
text-decoration: none;
}

ol#controls li.current a {
background: #8B3892;
color: #fff;
}

ol#controls li a:focus, #prevBtn a:focus, #nextBtn a:focus {
outline: none;
}

span.price {
font-size: 14px !important;
}

/* // Easy Slider */
</style>

<?php $_productCollection=$this->getLoadedProductCollection() ?>
<?php if(!$_productCollection->count()): ?>
<div class="note-msg">
<?php echo $this->__(‘There are no products matching the selection.’) ?>
</div>
<?php else: ?>

<?php // Grid Mode ?>
<div class="listing-type-grid catalog-listing">
<?php $_collectionSize = $_productCollection->count() ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if($i++%7==0): ?>
<div id="slider">
<ul>
<?php endif ?>
<li><div id="featured-product" class="tab<?php echo $i ?>">
<div class="featured-product-top"></div>
<div class="featured-product-body">
<div style="width:270px;float:left;">
<h2>Featured Product</h2>

<?php echo $this->htmlEscape($_product->getName()) ?><br>
<?php echo $this->htmlEscape($_product->getDescription()) ?>
<span class="featured-price"><?php echo $this->getPriceHtml($_product, true) ?></span>
<p><?php echo $_product->getDescription() ?></p>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img src="skin/frontend/3graces/default/images/hp_moredetails_btn.gif"></a>

</div></div>
<div style="width:150px;float:left;" class="featured-bg">
<div><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<img src="<?php echo $this->helper(‘catalog/image’)->init($_product, ‘small_image’)->resize(90, 90); ?>" width="90" height="90" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
</a> </div>
</div>
<div class="clr" /></div>

<?php if ($i%7==0 && $i!=$_collectionSize): ?>

<?php endif ?>
<div class="featured-product-btm"></div>
</div></li>
<?php endforeach ?>
</ul>
</div>

<script type="text/javascript">decorateGeneric($$(‘.grid-row’), [‘last’, ‘odd’, ‘even’]);</script>
</div></table>
<?php endif; ?>
[/php]

Step 3: Add necessary files to /js directory which is located at the root.
If it doesnt already exist, create a /jquery directory within the /js directory. So your path will look like this: root/js/jquery. Add the following files to the new directory.

  • easySlider1.7.js
  • jquery.1.2.3.js
  • add jquery no conflict which prevents jQuery from screwing up prototype which is built into Magento. Make sure that it is added to the very end of the jQuery library file.

[js]
jQuery.noConflict();
[/js]

Step 4: Update page.xml to reflect new /js files that were added. The path to page.xml is: /app/design/frontend/yourtheme/layout/page.xml
[html]
<action method="addJs"><script>jquery/jquery.js</script></action>
<action method="addJs"><script>jquery/easySlider1.7.js</script></action>
[/html]

Step 5: Update header.phtml to call Slider function. The path to header.phtml is: /app/design/frontend/yourtheme/template/page/html/header.phtml
[js]
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#slider").easySlider({
auto: true,
continuous: true,
numeric: true,
speed: 1000
});
});
</script>
[/js]

Step 6: Add the featured products block to your homepage using the Magento CMS. The category ID can be obtained from the category ID of the featured products category that you setup in Step 1.
[html]
{{block type="catalog/product_list" category_id="173" template="catalog/product/featured-products.phtml"}}
[/html]

Voila! You now should have a featured products slider powered by jQuery and Easy Slider on your homepage. Any products that you add to the featured products category, will show up in this slider. And each subsequent product that is added, will increase the numbered navigation along the bottom.

  1. Jake, this looks great. Will give it a shot this week on my test store and then hope to add to production. I had tried jquery a few months ago, but wasn't aware of the no-conflict mode, looking forward to trying this out.

    Thanks
    Mike

    • I'm also looking for a way to view 3 products within the same window – but then scroll next/prev one product at a time! did you ever figgure this out?? if so, would you please please please help πŸ™‚

  2. I'm rather curious how you got this to work happily with the prototype engine. I had tried using both easyslider and anything slider and neither of them worked until I removed the ref. to prototype.js. Which is, at this stage in Magento's life, not a usable option.

    I have all the usual no_conflict jazz implemented. Nothing worked. I tried switching order of script loading and nothing worked except when I removed prototype.js from the page.xml call. Then both easyslider and anythingslider worked like a charm.

  3. Have you tested 3graces.com in IE7? It is not displaying properly in IE7? Internet Explorer 7 is apparently ignoring the overflow:hidden, so all the products in the category are being displayed on the page simultaneously. It works perfect in Firefox/IE8/Safari, but it obviously must also work in IE7. I have spent some time troubleshooting this, but to no avail. Is anyone aware of a solution?

  4. This is excellent! I like using jQuery for sliding content and lightboxes. When implementing into Magento, traditionally it hasn't been a dynamically controlled feature but instead it was a series of graphics. This is an great tutorial, Thanks!

  5. Does anyone know how to implement mousewheel instead of slider?
    Here is a plugin.

    plugins.jquery.com/project/mousewheel
    This also works with jquery 1.3.2. I think it would be great for anyone for magento users including Magento1.4.
    I'm stuck with this.

  6. i tries this in M1.4 and it didn't work. I got the error message

    Fatal error: Call to a member function getTypeInstance() on a non-object in …./app/code/core/Mage/Catalog/Block/Product/Abstract.php on line 74

    which is produced through
    {{block type="catalog/product_list" category_id="173" template="catalog/product/featured-products.phtml"}}
    or the /featured-products.phtml

  7. Thank you very much for this lovely tutorial πŸ™‚ It is working in Magento 1.4.1.0 like a charm.

    NB: Note that in step 4 the included jquery file in the page.xml file is jquery.js, so make sure to rename your jquery-1.2.3.js file to jquery.js before you place it under the /js/jquery/ directory

    • Me also facing same Issue the slider is not showing up. I can see the products but no slider I am using magento 1.4.2.0 Any help.

      Advance thanks
      Saleem

  8. This is great and just what I was looking for. It works fine for me in magento 1.4, only problem i'm having is that the when the slider gets to number 4 and 5, it shows 2 slides at the same time, and then the last 2 slides are blank.

    Not quite sure what is going on with it, anybody got any thoughts?

    • Ah I figured it out, in the code
      <div id="slider">

        is inside the foreach loop, so there are multiple div id#sliders on the page causing problems. Change the for loop to:
        <div id="slider">

        <?php // Grid Mode ?>
        <?php $_collectionSize = $_productCollection->count() ?>
        <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if($i++%7==0): ?>

        <?php endif ?>

      • the Bug is here: <?php if($i++%7==0): ?>

        If there are more than 7 products the script write a TAG that crash slider.

        Use this code:

        <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if($i++%$_collectionSize==0): ?>
        <div id="slider">

        <?php endif ?>

  9. hi , can anyone give me idea if i could get the products name on the buttons than to show the numbering?
    any help would be very much appreciated..

  10. Hi its not working for me in magento 1.4.1.0
    I just see the featured products below each other and no scrolling .

    Anybody got this working under magento 1.4.1.0. or knows a slider effect that is working??

    Kind regards,

    Paul

  11. hi
    I am completely new to this. Can someone tell me which file is referred to as the jQuery library file???
    Any help much appreciated
    Thanks

  12. followed the six steps but no matter what cannot products in the category are being displayed on the page one beneath the other… any ideas?

  13. Hello, thanks for sharing this information have been very useful!
    I am using Magento 1.4.1.0 and I have two problems.
    The main thing is that the description does not appear, I've listed the array and this field really does not make it. I tried using "$ this-> getProduct ()" but without success.
    The second problem is that it does not work in IE 7, but that is acceptable to the Microsoft case.
    If you can give help.

    Thanks!

  14. it is indeed a great tutorial .But in IE7 it gets break down.ie all products are shown in one line.if you could tell any
    sol. for this .it will be terrific.

    thanks,

  15. Thanks for this great tutorial. Its working for me in v1.4. I just wanted to share a 'problem' that i faced & which baffled me before I finally found out what was going wrong. My slider was working just fine when all of a sudden the contents products started getting mixed up & the container on the right shifted down. All this when I had made no changes to the code.

    Basically what was happening was that this code allows or assumes that you would be using a max of 7 featured products (line 127) <?php if($i++%7==0): ?> .. so if you exceed that, the code basically creates another slider using div/ul. Solution is to either keep your products no more than 7 .. or raise the number to the max number of products that you would want to slide. Just don't get carried away with having number larger than required as you would be creating extra entries.

    Small thing .. but it might save someone hours of debugging. Hope this helps. Thanks.

  16. i am using magento 1.4.1.1 and have followed all the steps but the slider doesn't show. the products are displayed in the page one below another repeting the 'featured product' title.

  17. This is great – but how would i feature this slider OUTSIDE of the 'content' area? So if i was to create a new reference in the template?

  18. I'm stuck on the very last step

    Step 6: Add the featured products block to your homepage using the Magento CMS.

    how do i do this? (im very new to magento, so forgive my stupidity)

    • You had to go to admin panel. Under CMS->Pages, u can find the home page of your website. Then under content u had to put the code for your block.
      For Ex like:

      {{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" template="catalog/product/list.phtml"}}

  19. it's ok for me on 1.4.1 .
    First i have no slider , but i modify jQuery("#slider").easySlider({ with $("#slider").easySlider({

    and work's now.

  20. does anyone tested on Magento 1.5.0.1??? I am also having same issue as most of them… doesn't show the slider..

    I have follder every steps.. after i added to my home page… i don't see the slider appear.. I know it pushed down my other products.. … PLEASE HELPPPPPP

  21. I've discovered an excellent module with regard to opencart known as "Opencart Custom T-Shirt Design".
    http://codecanyon.net/item/opencart-custom-tshirt

    “Custom T-Shirt” Module is an open cart module which is used to design or customize T-shirts. By this module customers can design the t-shirt as per their need such as add Text , Number or Logo by different Color, Font Size, Alignment and many more.

  22. Check product slider from NWDthemes – nwdthemes.com/extensions/product-slider/ . It allow to add responsive slider to showcase
    your Bestsellers, Featured or Most viewed products in almost any place on site

  23. How to set particular number of product i.e from admin we write {{block type=”catalog/product_list” category_id=”173″ template=”catalog/product/featured-products.phtml” product_count=”8″}} means we want to slide only 8 product of that particular category (product_count=”8″) not all product so what should we do.

LEAVE A REPLY