Magento for Designers: Part 6

Magento for Designers: Part 6

Tutorial Details
  • Technology: Magento
  • Difficulty: Moderate
  • Estimated Completion Time: An hour
This entry is part 6 of 8 in the Magento for Designers Session
« PreviousNext »

Magento is a stunningly powerful e-commerce platform. In this miniseries, we’ll learn how to get started with the platform, get to know the terminologies, set up a store and all related aspects of it and finally learn how to customize it to make it our very own.

In this sixth part, we’ll be building one of the most reader requested feature: getting a featured product slider in the home page. Excited? Let’s get started!

The Full Series


A Quick Recap

In the last part, we built the product information page and took a good look at how data is extracted and displayed in a typical Magento theme.

We also took a look at the various core methods that our theme uses to get various media and data about our product. I hope you remember at least a sliver of that information because we’ll be using it again today.


What are We Building Today?

Today, we’ll build the all important home page. As always, I’ll keep it as simple as possible and omit any extraneous features. Different stores have different content in their home pages. One of the most popular and definitely one of the most requested features is displaying a JavaScript powered content slider featuring special featured products. I know it sounds interesting and you’re itching to get started. Let’s get started!


Goals for the Page

As with our other pages, our goals for this specific page are relatively simple. We want a simple slider that displays specific products in a neat slider. Thus, I’ve drawn up a short list of elements that I think it needs:

  • JavaScript slider on the home page
  • Slider needs a catchy image on the left to attract attention to the product
  • In the right portion, we’ll be including the product’s name, price and a short description
  • A mini navigation within the slider to switch between different slides
  • And most important of all, not rely on an extension to obtain the featured items

That’s it. I want to keep it as simple as possible and thus opted to not over do it. We will of course need to work with both the front end and back end to accomplish all our goals.


The Basic Look

The page basically has to look like so:


Step 1 – The HTML

We’ll first look at the HTML for the content part alone. I’m assuming you’re fairly fluent in HTML and CSS so I’ll skip to the fairly important parts.

<div id="home-slideshow">

    <div class="slide">

        <div class="product-image">
            <a href="#" title="Zonda F">
                <img src="images/zonda.jpg" alt="Zonda F" title="Zonda F" />
            </a>
        </div>

        <div class="product-details">
            <h2>Zonda F</h2>
            <div class="price">700,000.00 </div>
            <p>The Pagani Zonda is a mid-engined sports car produced by Pagani in Italy. </p>
        </div>

    </div>

<!-- More slides for successive products -->

</div>

At this point, this is all pretty basic. home-slideshow is the container we’re wrapping the entire slideshow with and will be passed on to the JavaScript responsible for the slider.

Each slide is wrapped by a div called slide. Inside we have two floated containers: one for the image and the second for the information about the product itself.

That’s about all there is to it. We can move on to the styling part now.


Step 2 – The CSS

/* Home page */

.slide {
    clear: both;
    overflow: auto;
    padding: 10px;
    width: 898px;
    border: 1px solid #E1E1E1;
    background: #F6F6F6;
}

.product-image {
    float: left;
    width: 650px;
    padding: 10px;
    border: 1px solid #E1E1E1;
    background: #FAFAFA;
	
}

.product-details {
    float: left;
    width: 190px;
    margin: 0 0 0 20px;
}

.product-details .price {
    margin: -15px 0 0 0;
    color : #7db000;
}

.product-details p{
    margin: 10px 0 0 0;
}

#nav {
    position: absolute;
    top: 255px;
    right: 250px;
    z-index: 2000;
}

#nav a { 
    margin-right: 10px; 
    height: 5px;
    width: 5px; 
    z-index: 1000;
    float: left;
    text-indent: -9999px;
    cursor: pointer;
    border: 1px solid #fff;
    background:  #eee;
}

#nav a:hover { 
    border: 1px solid #fff;
    background:  #999;
}

#nav a.activeSlide { 
    border: 1px solid #444;
    background:  #333;
}

Nothing fancy here. Very basic CSS to place the elements in position.

The only point to note is the declaration for the class activeSlide. This class is assigned to the current slide’s respective navigation anchor.


Step 3 – Setting up Our Store

The first thing we need to do is setup a separate category to which we can assign all the products that we need to appear on our slider. Click on the catalog menu and then on categories. Choose add new root category.

Magento for Designer: Part VI

Key in a name of your choice and then choose no for the is active dropdown. We don’t want it displaying in other parts of the site.

Magento for Designer: Part VI

Once the new category has been saved, you can find the ID it has been assigned to. Note this down. We’ll need it for later.


Step 4 – Creating Our Core Code

We’ll be using a single template file to pull out the relevant information, format it as needed and display everything. You have complete freedom regarding the name and location of this file but in the interest of keeping it simple, I’ve named it featured.phtml and placed it in catalog/product/featured.phtml. Remember, this goes into your template folder, not your layout folder.

The contents of the file looks like so. Don’t worry about the length, we’ll look at each part works after the code.

<?php 
$cat_id = "2"; 
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));

if(!$_productCollection->count()): 
    echo $this->__('There are no products matching the selection.'); 
else: ?>

    <div id="home-slideshow">
        <?php $i=0; foreach ($_productCollection as $_product): ?>

        <div class="slide">

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

            <div class="product-details">
                <h2><?php echo $_product->getName(); ?></h2>
                <div class="price"><?php echo number_format($_product->getFinalPrice(),2);?> </div>
                <p><?php echo $_product->getShortDescription(); ?></p>
            </div>

        </div>
        <?php endforeach ?>

    </div>

<?php endif; ?>

Here’s the first piece:

$cat_id = "2"; 

First, we’ll need to specify the ID of the category from which we want to pull in our products. In our case, this will be 2, the ID of the category we just created.

$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));

This looks slightly complicated but let me assure you, it’s very simple. To put it in layman’s terms, we essentially pass in the ID of the category we need through the variable cat_id and ask Magento to retrieve the name, price, image and short description of the products that match. Right now, I’d advise you to not fiddle with the rest unless you’re well versed in MVC and other aspects of Magento’s architecture.

if(!$_productCollection->count()): 
echo $this->__('Sorry, no items matched your selection criteria'); 
else: ?>

We will need to check whether we have matching items, don’t we? That’s what we’re doing here. Just making sure we have matching items. If not, we display an error.

<?php $i=0; foreach ($_productCollection as $_product): ?>

Let’s start looping through our product collection!

<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(650, 250); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
<h2><?php echo $_product->getName(); ?></h2>
<div class="price"><?php echo number_format($_product->getFinalPrice(),2);?> </div>
<p><?php echo $_product->getShortDescription(); ?></p>

If you’ve read the previous part of this series, this code must be pretty familiar to you. We use Magento’s baked in methods to retrieve media pertaining to our product.

The second snippet may look a little different because instead of using slightly more abstracted methods, I used closer-to-bone methods to directly acquire the data we need.

If you’re feeling a little confused here, just go through the above code one more. The name of the methods are pretty explanatory. The only stumbling block should be how we acquire the image itself: we ask one of our helpers to acquire the image and resize it to the size we need. Nothing more to it.

Note that we open and close a slide div each time our loop runs. This is to encapsulate each product’s information with a container element to make it easy for us to create a slider later.

And with this, our base PHP code is complete.


Step 5 – The JavaScript

It makes no sense to completely reinvent the wheel here since the core focus of this series is on Magento. Thus, we’ll be using an excellent plugin to create our slider today: the jQuery Cycle plugin.

But first, go to root/js and create a folder named cirrus. Different people have different methods of organizing here. I prefer having a theme’s JS separate instead of organizing them by source. We’ll need a copy of jQuery [1.4 hopefully], the latest cycle plugin and a custom script file, which I’m naming mocha.

Here’s the content of our JS file.

(function($) { 

$(document).ready(function(){
     $('#home-slideshow')
	 	.before('<div id="nav">')
		.cycle({
		fx: 'fade',
		speed:    1500, 
 		timeout:  5000,
 		pause: 1,
 		delay:  5000, 
		pager: '#nav'
	});
});
 })(jQuery)

First up, notice that we’re wrapping everything inside a (function($) { //Code })(jQuery) block. This is make sure jQuery doesn’t conflict with any other included library.

The document ready block, as always, is to make sure that our code runs only after all the page’s assets are loaded since Magento likes to put our JavaScript files at the top.

Initializing the plugin is pretty simple. Just type in the ID of the main container, home-slideshow in our case, and away we go. I’ve just tweaked some of the options to make it do our bidding.

Something to take note of is that we’re dynamically creating and positioning our container for the navigation elements inside the DOM with JavaScriptS. This element is only needed if JavaScript is enabled. If it isn’t, we don’t want empty, useless code in our page.

We also pass in the selector the navigation container to the slider plugin.

That’s it. The JavaScript part of our code is done.


Step 6 – Setting up Our Home Page

Now that all our groundwork has been completed, we can finally configure our home page now.

Magento for Designer: Part VI

Click on the CMS menu and choose the pages option. Click on home page in the resulting page.

Magento for Designer: Part VI

The first tab should be pretty self explanatory. Make sure that our page is enabled.

Magento for Designer: Part VI

In the content tab, make sure you enter the following after disabling the editor:

{{block type="catalog/product_list" block_id="featured.products"  template="catalog/product/featured.phtml"}}

This is Magento’s method of including required blocks. We just give it an ID to refer it by and point it to our custom template.

Magento for Designer: Part VI

In the design tab, change the layout template to 1 column since we’re using it for everything and then enter the following into the layout update xml field.

<reference name="head">
          <action method="addCss"><stylesheet>css/home.css</stylesheet></action>
          <action method="addJs"><script>cirrus/jquery-1.4-min.js</script></action>
          <action method="addJs"><script>cirrus/cycle.js</script></action>
          <action method="addJs"><script>cirrus/mocha.js</script></action>
</reference>

Remember how when building the product page, we included custom CSS in the page’s layout file to make sure the entire codebase isn’t loaded for every page? We’re doing essentially the same here. Instead of creating a XML file just for this, Magento lets us enter this directly through the interface.

We just load our custom CSS, our JS libraries and the script file that contains all our code.

Click on “save” to save up all our progress, empty Magento’s cache and watch the beautiful slider on your home page slide through in its excruciatingly lovely glory!


What We’ll be Building in the Next Part

.. is completely up to you. I’ve already covered almost all relevant information for customizing any view and going through each and every view is completely counter-intuitive. Thus, I’ve concluded that all that’s left is how to create a custom module and I think by then this series will have runs its course. I may write a quick Magento tweaks and optimization how-to if enough people show interest for such an article.

If you do feel that something else needs to be covered before this series comes to a close, don’t hesitate to leave a comment below to let me know!


The Last Word

And we are done! Today, we learnt how to create a custom home page complete with a slider displaying featured products. We also learnt to do all this without relying on a third party extension and instead coding all this ourselves. Hopefully this has been useful to you and you found it interesting. Since this is a rather new topic for a lot of readers I’ll be closely watching the comments section so chime in there if you’re having any doubts.

Questions? Nice things to say? Criticisms? Hit the comments section and leave me a note. Happy coding!

Siddharth is Siddharth on Codecanyon
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Ep

    OMG LEARNT!

    • Jenn

      This kills me…

      • grammar nazi

        Lol, I was going to comment just to say “OMFG ‘Learnt’???” but you did it for me. hahaha

  • Tiby

    Great post … i’m just faceing with this problem right now :D :)) and this is my salvation :D thx :D

  • http://diegoprates.com.br Diego Prates

    Very nice article !!!!!

    Thanks x)

  • http://newfasttowing.com Hassan

    Why is Magento so slow? I installed it on the website and it operates pretty slow.

    • http://envexlabs.com Matt Vickers

      Cause it’s a bloated P.O.S :D

      All I know is that mediatemple removed it from it’s 1-click installs because it took a whole lot of resources.

      • Hassan

        Yea, I am with MT and I called them regarding this matter and the agent told me don’t go with Magento because it is super slow and what not. This series is pretty cool.. but Magento is just too damn slow (lol) I am sure they can somehow make it faster so more people would use it.

    • Zoran

      Don’t use it for small or simple shop, you have much better alternatives than Magento… but if you really have to and your client needs speed, then he/she should be ready to pay for dedicated server and you also have to consider looking at caching of Magento.

      • http://www.ssiddharth.com Siddharth
        Author

        Well, Magento isn’t anywhere close to small. It’s a big lumbering beast and it really breaks most shared hosts. I’d go with a VPS if I were anything close to serious on running Magento.

        I wouldn’t really call it a bloated POS. :)

  • http://inwebdeveloper.com inwebdeveloper

    wow great tutorial..

  • http://www.jsxtech.com Jaspal Singh

    Nice tutorial.
    Thanks Siddharth for sharing the article.

  • http://www.jordanwalker.net Jordan Walker

    Great tutorial, thanks.

  • sauware

    Thank you very much for the lessons, hopefully you have more time to go more often.

    • http://www.ssiddharth.com Siddharth
      Author

      Sure, just let me know which topic you’re really looking forward to and I’ll try my best to cover it.

  • http://www.ysbeer.com Robbert

    Thank you very much for these series :) Really great!

  • http://blog.hirephpdeveloperindia.com Nehal

    Hi Siddharth,

    Thanks for writing wonderful tutorial. I am big fan of net.tuts and i am following your tutorial from 1st of it.
    It would be great if try to cover developing an extension or you can share any links which explain proper api if magento functions and class etc.

    Because i guess magento documentation still not very powerful on which we can write our own extension.

    • http://www.ssiddharth.com Siddharth
      Author

      Duly noted. We’ll look at extensions next.

      Thanks for reading.

  • Josh

    The default install of magento includes both prototype.js and scriptaculous, Although I like Jquery better, why include a whole other js framework when you could spend a little extra time and work with what is in place?

    • http://www.ssiddharth.com Siddharth
      Author

      Because we can? Isn’t that why we do most things? To see how far we can push them. :)

  • tim

    Hi,

    nice tutorial, thanks a lot.

    As an Idea for a further tutorial, how a about showing a preview of the product in a modalbox maybe with prev next buttons.

  • http://www.z115corporation.com Tien

    Nice tut!!! Thanks

  • http://solith.com.pl Krzysiek Dróżdż

    Hmm, are you sure that it will work with Magento 1.4?

  • http://www.cinderblock.com Tyler

    Awesome tutorial, I think I might be able to make use of this with my project. I have about 55 stores on sub domains and sub root categories, and all products for each store runs through the main category or root. I now want to be able to set up a main general store and a main wholesale store that pulls all products from all 55 stores. Is there a way to create, from the main root category, two separate store views that pulls all the data/products? I have been able to create both general and wholesale stores, but they are not able to communicate yet ie. i change something in general and it gets updated automatically in the wholesale store.

    • http://haydenhancock.com Hayden Hancock

      @Tyler,

      You can set a website/store to use a particular category through System -> Manage Stores

  • http://no.com mark

    This is good, but please talk more about how to upgrade a theme once a new version of magento comes out and they change some things. Do you need to do it all over again? Is there a simple way to bring a theme up to date without starting over each release?

    • http://haydenhancock.com Hayden Hancock

      @mark,

      It really depends on how you create your theme. As with any software upgrade there are going to be changes that are needed to your existing themes. This is no different for Magento and you can bet you will have to change some aspects of your theme. If you are creating a theme using the default theme and making all changes to the default theme folder than Magento WILL overwrite this. Any core changes will also be overwritten by any upgrades. There are a lot of ways around this and Google will be a good resource. I’ve found that the official Magento documentation is lacking though. The Magento Forums and chat are always a good place to start.

      Magento is indeed a beast and I would recommend running it on a VPS. I use Media Temple and have a dv server and it runs fine on it.

  • http://health.elct.or.tz Kizito Mrema

    Yup! This one was fun doing in NetBeans a la Ubuntu lucid.

  • Nicolas Vanden Bossche

    First of all, thanks for these amazing tutorials, I really enjoyed them.

    One thing I would like to know, is how to change the layout/style of the entire shop according to in which category you are currently browsing.
    E.g. a different look-and-feel if you are shopping for men’s clothing or women’s clothing.

  • Marc

    Hello Sid,

    Great tutorial

    i have added all what you have described, but still it shows “Sorry, no items matched your selection criteria”

    Help please??

    Thanks
    Marc

    • http://www.charleyramm.co.uk Charley

      In Admin > System > Index management

      Check if your indexes have been rebuilt since adding the sample data (example products and so on).

  • http://magento.acorespro.com/ Marco Ferreira

    Hi,
    When i have this slideshow active my top menus doesnt show the sub-menus
    http://magento.acorespro.com/
    you can see the slideshow working on the main page, select one of the blue buttons and when you are on another page mouse over it and you will see it has an effect to show sub-menus ( with sub-categories of the products).
    It seems to be some sort of conflict with jquery 1.4 min but i dont really know how to fix it. any ideas?

  • Mayan

    Hi Sid,

    Just went through the whole magento series and it really was a great refresher for me. Looking forward to reading more articles from you! :D

  • http://www.melayumall.com Khai

    Hi.
    I’m newbie in Magento.
    So, I don’t understand something.
    I don’t understand about HTML code.

    For the last action, we need to put a code in layout xml field.
    The code will function as to call “.css” file, “javascript” file and “jquery” file.
    How about the HTML code?
    What is the function of HTML code that shown in the first step?
    What I need to do with it?

    • http://charleyramm.co.uk Charley

      I think the html code given is just an example of what will be created by building the tutorial. It is not necessary to include it in the theme folder on your server.

  • __fabrice

    Hi,

    Very nice tutorial, But, I have a problem using the $cat_id. My id of a particular category is 3.

    I do this :
    $cat_id = “3″; // or $cat_id = 3;

    And I’ve any product :(. When I set 0, I’ve all the products in all categories. But, with a good ID : no products.

    Any idea? Thanks.

    Fabrice

  • Ryan

    This is exactly what I was looking for… just one question. How would I go about echoing a custom attribute into the slider?

    Thanks..

  • http://www.nospam.com NewGuy

    Does this work for the Enterprise edition?

  • http://www.solarfabric.com Gary

    Hy , This is exactly what I need for my site . I want to test it first separately in a cms -page Is this possible to not let it show on home page until I finish building it . Also I am currently using Magento 1.5.1 Will it work for this version?

    • http://www.electrical2go.co.uk Ian

      Just added to magento 1.5.1.0 and it works, you may have to add the ” jQuery.noConflict(); ” to the end of the ” jquery-1.4-min.js ” file to stop any small errors

      Ian

  • http://www.electrical2go.co.uk Ian

    Thanks for the tutorial.

    Nice and quick to impliment. Works well on Magento 1.5.1.0, had a slight error with it breaking other area’s of the homepage so I added ” jQuery.noConflict(); ” to the end of the ” jquery-1.4-min.js ” file and it works fine now.

    Many thanks – Ian

  • http://www.hiccupsolutions.com ami

    Wow nice information you have here.Thanks for sharing this interesting blog. Actually google made searching of information easy on any topic. Well keep it up and post more interesting blogs.

  • http://www.bytedust.com Bytedust

    Thanks so much for the workaround for JQuery and Prototype.js conflict!

  • http://www.websourceblgo.com websourceblog

    Nice tutorial.

    Thanks,

  • Vincent

    This just doesn’t work at all for me:S
    I uploaded your files, changed the cat_id for mine.
    The home page content is just blank and it doesn’t even load the page featured.phtml
    Anyone know why?

  • krite

    If i want create and design same in this site http://web-experiment.info/magento14/web_experiment/ please tech me. thank you for tech me.

  • Phill

    Great tutorial but I get a 1> 2> etc for each product place in the category, something to do with the “for each” in featured.phtml?

    Magento 1.6.1

  • http://www.crewffort.com Tommy

    I tried put a menu with Categories, using the example of default theme, but does not show the categories.
    Does anyone know how to do this?

  • http://www.pbobak.co.uk Bobby

    Hello,
    I tried this method but it simply does not work. The count() returns 0 even if I have specified the correct category ID. Is there any other way to make it work on magento v.1.6 ?

    • dekker

      I got the same problem, it echoes the ‘There are no products matching the selection.’ message

      But there is products in my databse !

      When I do a print_r on the $_productCollection
      I can see my products with the correct id I specified.

      Could anyone help us with that ? We ain’t the only one facing this pb.

      Thanks :)

  • http://webdesignergeeks.com Ajay Patel

    Hello sid, First of all thanks this tutorial series help me a lot to learn magento.

    Now i have issue with the slider, i have created category and page as per 6th tutorial, also added the product with image for featured category but still i am not able to see the slider on my theme.
    please help me how can i solve this…. can’t wait for next tuts….

  • Erik

    In what we’ll be building next you said: “Thus, I’ve concluded that all that’s left is how to create a custom module and I think by then this series will have runs its course.”…did this get dropped from the series, I was kinda curious about this? The next part is optimization…but nothing on a custom module…or am I missing something?

  • Abdelfatta Zine

    Thank’s this tutoriel is great

  • towhidd

    How I will show add to cart functionality?

    Thanks

  • Typer

    Hey!
    How can I limit the items, that are fetched?