Magento Themes and Magento Extensions

Listing products by attribute

Share

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

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

8 4

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.

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:

8

And how many columns:

44

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”

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.

Leave a Reply

You must be logged in to post a comment.