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>
- First of all define product to New by setting definitions “Set Product as New from Date ” and “Set Product as New to Date ”.
- Magento already has custom listing new product template which you can find in app/design/frontend/pro/YourTheme/template/catalog/product/new.phtml
- Define where do you want to show new.phtml template by block structure in layouts xml files.
- Insert the new block
- 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
- This is all you need to do.
<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>
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.
- First of all we need to create attribute for product in Magento administration
- Create dropdown (Yes/No) type attribute and call it “Promotion”
- If you want to show attribute for all product in Magento Administration associate it with default Attribute set
- Duplicate template/catalog/product/list.phtml file, put it in the same directory as list.phtml and rename it to promotion.phtml
- 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" />
- 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.
- Create new directories in app/code/local/Mage/Catalog/Block/Product/List/Example.php
- Copy content of app/code/core/Mage/Catalog/Block/Product/List/Promotion.php and paste it to Example.php
- In Example.php change:
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.
8 comments
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 🙁
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..
How can I show label “new” on the listing if my product is set as new? Tnx
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.
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
can you tell me the full of the procedure of this kind of workk !!! e g in which file have to edit
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.
Thanks, worked for me well.