Attributes Products Filter – Show Products with Custom Attribute on Home Page

attributesfilterhomeproducts

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?

Best Answer

If you want to show them only on home page why not to use a different approach? Just create three categories - Excellent, Good and Average. Assign necessary products to that categories accordingly.

In CMS - Pages - Homepage just add this:

{block type="catalog/product_list" category_id="8" template="catalog/product/custom.phtml"}} 

Change the category_idto the id of Excellent category. Customize catalog/product/custom.phtml according to your needs.

Thats it.

Related Topic