Listing products by attribute

Listing products by attribute

This is step by step tutorial for the beginners that will show you how to list products by attribute.
I will start with functions that Magento already has built-in: New products and Promotional products.
All the instructions below is made from Magento Professional 1.9.0.0

Listing products with attribute New

You can find settings for defining New attribute in Magento administration under Manage products in Products management. For this tutorial I will use CMS page called Homepage as example.
In CMS Page layout -> Layout Update XML is defined template for CMS page by

<reference name="content">
 
<block type="core/template" name="home" as="home" template="page/home.phtml" >
 
</block></reference>
  1. First of all define product to New by setting definitions “Set Product as New from Date ” and “Set Product as New to Date ”.
  2. Magento already has custom listing new product template which you can find in app/design/frontend/pro/YourTheme/template/catalog/product/new.phtml
  3. Define where do you want to show new.phtml template by block structure in layouts xml files.
  4. Insert the new block
  5. <reference name="content">
     
    <block type="catalog/product_new" name="home.catalog.product.new" 	alias="product_new" as="newproducts"  	after="cms_page" template="catalog/product/new.phtml">
    <action method="setProductsCount"><count>8</count></action>
    <action method="setColumnCount"><count>4</count></action>
    </block>
     
    </reference>
  6. Define where do you want to show the block with template new.phtml by inserting
    < ?php echo $this->getChildHtml('newproducts') ?>

    In our case this is custom Homepage template located in template/page/html/home.phtml

  7. This is all you need to do.

Note:
Content is a core/text_list type and it can hold almost every block type.

You can set how many products you want to show:

<action method="setProductsCount"><count>8</count></action>

And how many columns:

<action method="setColumnCount"><count>4</count>4</action>

Listing products with attribute Promotion

Listing products with attribute Promotion is also already built-in function. This is slightly different from listing new products.

  1. First of all we need to create attribute for product in Magento administration
  2. Create dropdown (Yes/No) type attribute and call it “Promotion”
  3. If you want to show attribute for all product in Magento Administration associate it with default Attribute set
  4. Duplicate template/catalog/product/list.phtml file, put it in the same directory as list.phtml and rename it to promotion.phtml
  5. Define block location in layout XML file. In our case this is the CMS page “Homepage” 
    <block type="catalog/product_list_promotion" name="promotion" template="catalog/product/promotion.phtml" />
  6. Define where you want to show the block with template promotion.phtml by inserting
    < ?php echo $this->getChildHtml('promotion') ?>

    In our case this is custom Homepage template located in template/page/html/home.phtml

Listing products with custom template and dropdown Yes/No attribute

In this case we will use Magento’s function for Promotion product and slightly modify it.

  1. Create new directories in app/code/local/Mage/Catalog/Block/Product/List/Example.php
  2. Copy content of app/code/core/Mage/Catalog/Block/Product/List/Promotion.php and paste it to Example.php
  3. In Example.php change:
  4. class Mage_Catalog_Block_Product_List_Promotion extends Mage_Catalog_Block_Product_List

    to:

    class Mage_Catalog_Block_Product_List_Example extends Mage_Catalog_Block_Product_List

Also change:

$collection->addAttributeToFilter('promotion', 1)

to:

$collection->addAttributeToFilter('example', 1)

After that follow the same steps like I wrote for Promotion attribute.
I hope you’ll find this  tutorial helpful.

You made it all the way down here so you must have enjoyed this post! You may also like:

Enhanced Catalog Product Grid – Custom Attribute Filter Milovan Gal
Milovan Gal, | 55

Enhanced Catalog Product Grid – Custom Attribute Filter

How to create custom attribute source type? Domagoj Potkoc
Domagoj Potkoc, | 13

How to create custom attribute source type?

Add custom attribute to Magento’s PDF invoice Mladen Lotar
Mladen Lotar, | 66

Add custom attribute to Magento’s PDF invoice

8 comments

  1. Where can i define the block name of my promotion articles ? When i include the new products block in the sidebar i have the block name “New Products” but when i include the promotion products i havent got a block name there is nothing 🙁

  2. i am apply filter in the list.php file like this
    protected function _beforeToHtml()
    {
    $collection->addAttributeToFilter(‘listhidden’, array(‘eq’=> 0 )) //custom attrib ID
    ->addAttributeToSelect(‘*’)
    ->setVisibility(Mage::getSingleton(‘catalog/product_visibility’)->getVisibleInCatalogIds());

    it is work good but in the layer category filter they show incorrect product count
    can you please help for this..

  3. I have read your blog. I have some confusion in the step. please tell me how to display the featured product’s name in combo box? because I difficult to understand this steps. pls guide me or help me.

  4. I’m working on a project where products are added directly from category listing page. No need to go to product page. When i click on “addtocart” button, it goes to the product page and shows message “options required”. When i check view.phtml file, then i came to know that “getChildHtml(‘product_type_data’) ?>” is resposible for it. Hence, i want to know how i can load “getChildHtml(‘product_type_data’) ?>” on category product listing (list.phtml page). Plz help me

  5. can you tell me the full of the procedure of this kind of workk !!! e g in which file have to edit

  6. Hi,
    I want to list products in a static block on my home page by a specific dropdown attribute and value – eg. attribute: usedfor, value: diy.

    Can you tell me how I would adapt this code to do this please? It would be a really useful feature.

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=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.