cancel
Showing results for 
Search instead for 
Did you mean: 

Show products with custom attribute in home page

Show products with custom attribute in home page

In my store I create an attribute that code is admin_rating for every product and it has three values: Excellent, Good and Average. Then in home page I have a box that I want to show the products whose rate is Excellent.

 

I searched about it, I tested this solution but it show me this: "There are no products matching the selection. This is a static CMS block displayed if category is empty. You can put your own content here."

 

What I doing is:

In

app/code/local/Mage

I added Catalog folder and create this file in it:

app/code/local/Mage/Catalog/Block/Product/Featured.php


Then I added this code in Featured.php:

 

<?php

class Mage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_List {
    protected function _getProductCollection() {
        if (is_null($this->_productCollection)) {
            $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addStoreFilter()
            ->addPriceData()
            ->addTaxPercents()
            ->addUrlRewrite()
            ->addAttributeToFilter('admin_rating', array('eq' => 'Excellent'))
            ->addFieldToFilter('sku', array('like', '10'))
            ->setPageSize($this->get_prod_count())
            ->setCurPage($this->get_cur_page());
            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
            $this->setProductCollection($collection);
        }
        return $collection;
    }
}

And in

app\design\frontend\base\default\layout\cms.xml

added this code in it:

 

<cms_index_index translate="label">
    <label>CMS Home Page</label>
    <reference name="content">
        <block type="catalog/product_featured" name="product_featured" template="catalog/product/list.phtml">
        <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
            <block type="page/html_pager" name="product_list_toolbar_pager"/>
        </block>
        <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
        <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
        <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
        <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
        <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
        <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
    </block>
    </reference>
</cms_index_index>

Then because I want to show products in home page I added below code in home cms page in back-end:

 

<reference name="content">
     <block type="catalog/product_featured" name="product_featured" template="catalog/product/list.phtml">
         <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
            <block type="page/html_pager" name="product_list_toolbar_pager"/>
         </block>
         <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
         <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
         <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
         <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
         <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
         <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
     </block>
</reference>

Why it doesn't show products? Where am i wrong?

1 REPLY 1

Re: Show products with custom attribute in home page

Hi @E_Gdz

 

Install this module on your website, this is fully customised module you can make the settings from your admin panel and feature the products where you want on your website including Homepage. 


Click here to download the module.


Query solved? Accept as Solution.Thanks
Eric Baily