Show Products on Homepage Using Magento Filter Collection By Attribute,Field

Show Products on Homepage Using Magento Filter Collection By Attribute,Field

Today, Magesolution comes back with the Magento‬ ‪‎tutorial about some effective methods to show Products on Homepage Using Magento Filter Collection By Attribute,Field

First, explain how Magento filter a collection. Use this to explain how Magento filter a collection by attribute, field in a model, so that people can learn to write their own collections.

addAttributeToFilter() is a function that can be called on a collection in Magento. In
short, it adds a condition to the WHERE part of the MySQL query used to extract a collection from the database.
For example:

$_products = Mage::getModel ('catalog/product')->getCollection()

->addAttributeToSelect(array('name', 'product_url',

'small_image'))

->addAttributeToFilter('sku', array('like' => 'UX%'))

->load();

The above code would get a product collection, with each product having it’s name, url,
price and small image loaded in it’s data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

Filtering


We can filter our collections with the addFieldToFilter() function. In SQL you can think of these as your WHERE clauses.
Lets say we want to get a product by the sku PRODUCT001

$products = Mage::getModel('catalog/product')->getCollection();

$products->addAttributeToFilter('sku','PRODUCT001');

$product = $products->getFirstItem();

Lets try the Greater Than conditional. Simply supply an array with the conditional type and value.
Products greater than $100:

$products = Mage::getModel('catalog/product')->getCollection();

$products->addAttributeToFilter('price', array('gt' => '100'));

Magento gives us lots of filter conditionals to choose from see:

Filter Condition Codes

 Attribute Code     SQL Equivalent     Description 
eq =  
neq !=  
like LIKE  
nlike NOT LIKE  
in IN ()  
nin NOT IN ()  
is IS  
notnull IS NOT NULL  
null IS NULL  
moreq >=  
gt >  
lt <  
gteq >=  
lteq <=  
finset FIND_IN_SET()  
from >= for use with dates
to <= for use with dates
date   optional flag for use with from/to to specify that comparison value should first be converted to a date
datetime   optional flag for use with from/to to specify that comparison value should first be converted to a datetime

 

If an array is passed but no attribute code specified, it will be interpreted as a group of OR conditions that will be processed in the same way.

If no attribute code is specified, it defaults to eq.
addAttributeToFilter Conditionals

Equals: eq

$_collection-&gt;addAttributeToFilter('status', array('eq' =&gt; 1));

Not Equals – neq

$_collection-&gt;addAttributeToFilter('sku', array('neq' =&gt; 'test-product'));

Like – like

$_collection-&gt;addAttributeToFilter('sku', array('like' =&gt; 'UX%'));

One thing to note about like is that you can include SQL wildcard characters such as the
percent sign.
Not Like – nlike

$_collection-&gt;addAttributeToFilter('sku', array('nlike' =&gt; 'err-prod%'));

In – in

$_collection-&gt;addAttributeToFilter('id', array('in' =&gt; array(1,4,74,98)));

When using in, the value parameter accepts an array of values.
Not In – nin

$_collection-&gt;addAttributeToFilter('id', array('nin' =&gt; array(1,4,74,98)));

NULL – null

$_collection-&gt;addAttributeToFilter('description', 'null');

Not NULL – notnull

$_collection-&gt;addAttributeToFilter('description', 'notnull');

Greater Than – gt

$_collection-&gt;addAttributeToFilter('id', array('gt' =&gt; 5));

Less Than – lt

$_collection-&gt;addAttributeToFilter('id', array('lt' =&gt; 5));

Greater Than or Equals To- gteq

$_collection-&gt;addAttributeToFilter('id', array('gteq' =&gt; 5));

Less Than or Equals To – lteq

$_collection-&gt;addAttributeToFilter('id', array('lteq' =&gt; 5));

OR condition

$_collection-&gt;addAttributeToFilter(array(
array(
'attribute' =&gt; 'field_name',
'in' =&gt; array(1, 2, 3),
),
array(
'attribute' =&gt; 'date_field',
'from' =&gt; '2010-09-10',
)
));

Show New Product: Using addAttributeToFilter By Attribute

Step 1: In app/code/local/Mage/Catalog/Block/Product (if this folder does not exist, please create it). Create block file(Newproducts.php) that extend to

Mage_Catalog_Block_Product_List.

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

&lt;?php

class Mage_Catalog_Block_Product_Newproducts extends Mage_Catalog_Block_Product_List {
protected function _getProductCollection() {
 if (is_null($this-&gt;_productCollection)) {
 $todayDate = Mage::app()-&gt;getLocale()-&gt;date()-&gt;toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
 $collection = Mage::getResourceModel('catalog/product_collection')
 -&gt;addAttributeToSelect(Mage::getSingleton('catalog/config')-&gt;getProductAttributes())
 -&gt;addStoreFilter()
 -&gt;addPriceData()
 -&gt;addTaxPercents()
 -&gt;addUrlRewrite()
 -&gt;addAttributeToFilter('news_from_date', array('date' =&gt; true, 'to' =&gt; $todayDate))
 -&gt;addAttributeToFilter('news_to_date', array('or' =&gt; array(
 0 =&gt; array('date' =&gt; true, 'from' =&gt; $todayDate),
 1 =&gt; array('is' =&gt; new Zend_Db_Expr('null')))
 ), 'left')
 -&gt;addAttributeToSort('news_from_date', 'desc')
 -&gt;setPageSize($this-&gt;get_prod_count())
 -&gt;setCurPage($this-&gt;get_cur_page());
 Mage::getSingleton('catalog/product_status')-&gt;addVisibleFilterToCollection($collection);
 Mage::getSingleton('catalog/product_visibility')-&gt;addVisibleInCatalogFilterToCollection($collection);
 $this-&gt;setProductCollection($collection);
}
return $collection;
}
}

Step 2: In layout file(cms.xml) or any CMS Page add following code:
For layout file(cms.xml):

&lt;cms_index_index translate=&quot;label&quot;&gt;
 &lt;label&gt;CMS Home Page&lt;/label&gt;
 &lt;reference name=&quot;content&quot;&gt;
 &lt;block type=&quot;catalog/product_newproducts&quot; name=&quot;product_new&quot; template=&quot;catalog/product/list.phtml&quot;&gt;
 &lt;block type=&quot;catalog/product_list_toolbar&quot; name=&quot;product_list_toolbar&quot; template=&quot;catalog/product/list/toolbar.phtml&quot;&gt;
 &lt;block type=&quot;page/html_pager&quot; name=&quot;product_list_toolbar_pager&quot;/&gt;
 &lt;/block&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;empty&lt;/layout&gt;&lt;count&gt;6&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;one_column&lt;/layout&gt;&lt;count&gt;5&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_left&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_right&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;three_columns&lt;/layout&gt;&lt;count&gt;3&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;setToolbarBlockName&quot;&gt;&lt;name&gt;product_list_toolbar&lt;/name&gt;&lt;/action&gt;
 &lt;/block&gt;
 &lt;/reference&gt;
 &lt;/cms_index_index&gt;

For any CMS page(e.g: Home Page):
Go to CMS->Pages, edit Home page like this:
Add following code to field Layout Update XML:

&lt;reference name=&quot;content&quot;&gt;
 &lt;block type=&quot;catalog/product_newproducts&quot; name=&quot;product_new&quot; template=&quot;catalog/product/list.phtml&quot;&gt;
 &lt;block type=&quot;catalog/product_list_toolbar&quot; name=&quot;product_list_toolbar&quot; template=&quot;catalog/product/list/toolbar.phtml&quot;&gt;
 &lt;block type=&quot;page/html_pager&quot; name=&quot;product_list_toolbar_pager&quot;/&gt;
 &lt;/block&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;empty&lt;/layout&gt;&lt;count&gt;6&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;one_column&lt;/layout&gt;&lt;count&gt;5&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_left&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_right&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;three_columns&lt;/layout&gt;&lt;count&gt;3&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;setToolbarBlockName&quot;&gt;&lt;name&gt;product_list_toolbar&lt;/name&gt;&lt;/action&gt;
 &lt;/block&gt;
 &lt;/reference&gt;

Picture1

Show Bestsellers Product: Using addAttibuteToFilter By Attribute 

Similar to show new product on home page.
Step 1: In app/code/local/Mage/Catalog/Block/Product (if this folder does not exist, please create it). Create block file(Bestsellers.php) that extend to Mage_Catalog_Block_Product_List.
app/code/local/Mage/Catalog/Block/Product/ Bestsellers.php

&lt;?php

class Mage_Catalog_Block_Product_Bestsellers extends Mage_Catalog_Block_Product_List {

protected function _getProductCollection() {
 if (is_null($this-&gt;_productCollection)) {
 $storeId = Mage::app()-&gt;getStore()-&gt;getId();
 $collection = Mage::getResourceModel('reports/product_collection')
 -&gt;addOrderedQty()
 -&gt;addAttributeToSelect('*')-&gt;setStoreId($storeId)
 -&gt;addStoreFilter($storeId)
 -&gt;addUrlRewrite()
 -&gt;setPageSize($this-&gt;get_prod_count())
 -&gt;setCurPage($this-&gt;get_cur_page())
 -&gt;setOrder('ordered_qty', 'desc');
 Mage::getSingleton('catalog/product_status')-&gt;addVisibleFilterToCollection($collection);
 Mage::getSingleton('catalog/product_visibility')-&gt;addVisibleInCatalogFilterToCollection($collection);
 $this-&gt;setProductCollection($collection);
 }
 return $collection;
 }

}

Step 2: In layout file(cms.xml) or any CMS Page add following code:

For layout file(cms.xml):

&lt;cms_index_index translate=&quot;label&quot;&gt;
 &lt;label&gt;CMS Home Page&lt;/label&gt;
 &lt;reference name=&quot;content&quot;&gt;
 &lt;block type=&quot;catalog/product_bestsellers&quot; name=&quot;product_bestseller&quot; template=&quot;catalog/product/list.phtml&quot;&gt;
 &lt;block type=&quot;catalog/product_list_toolbar&quot; name=&quot;product_list_toolbar&quot; template=&quot;catalog/product/list/toolbar.phtml&quot;&gt;
 &lt;block type=&quot;page/html_pager&quot; name=&quot;product_list_toolbar_pager&quot;/&gt;
 &lt;/block&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;empty&lt;/layout&gt;&lt;count&gt;6&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;one_column&lt;/layout&gt;&lt;count&gt;5&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_left&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_right&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;three_columns&lt;/layout&gt;&lt;count&gt;3&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;setToolbarBlockName&quot;&gt;&lt;name&gt;product_list_toolbar&lt;/name&gt;&lt;/action&gt;
 &lt;/block&gt;
&lt;/reference&gt;
 &lt;/cms_index_index&gt;

For any CMS page(e.g: Home Page):
Go to CMS->Pages, edit Home page like this:
Add following code to field Layout Update XML:

&lt;reference name=&quot;content&quot;&gt;

&lt;block type=&quot;catalog/product_bestsellers&quot; name=&quot;product_bestseller&quot; template=&quot;catalog/product/list.phtml&quot;&gt;

&lt;block type=&quot;catalog/product_list_toolbar&quot; name=&quot;product_list_toolbar&quot; template=&quot;catalog/product/list/toolbar.phtml&quot;&gt;

&lt;block type=&quot;page/html_pager&quot; name=&quot;product_list_toolbar_pager&quot;/&gt;

&lt;/block&gt;

&lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;empty&lt;/layout&gt;&lt;count&gt;6&lt;/count&gt;&lt;/action&gt;

&lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;one_column&lt;/layout&gt;&lt;count&gt;5&lt;/count&gt;&lt;/action&gt;

&lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_left&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;

&lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_right&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;

&lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;three_columns&lt;/layout&gt;&lt;count&gt;3&lt;/count&gt;&lt;/action&gt;

&lt;action method=&quot;setToolbarBlockName&quot;&gt;&lt;name&gt;product_list_toolbar&lt;/name&gt;&lt;/action&gt;

&lt;/block&gt;

&lt;/reference&gt; 

Picture2

Show Products With Custom Attribute or Field

Creating a attribute – Featured

  1. Log into the Magento admin panel
  2. Navigate to “Manage Attributes” under Catalog -> Attributes -> Manage Attributes
  3. In the top right, click on “Add New Attribute”
  4. You’re now on the Attribute edit page and Properties tab. It looks like this:

Properties Tab

Picture3

Frontend Tab

Picture4

  • Attribute Identifier: This is the name of the attribute used by the system. Spaces can not be used in this field. We’ll type featured.
  • Scope: This determines the store level at which this attribute will save for all products. If you choose Global, and edit Featured for product 123 in store A to Yes, product 123’s featured in store B (and any other stores) will also update to Yes.
  • Catalog Input Type: This describes what kind of data the attribute will store. What’s set here determines how data entry for this attribute will take place. We’ll use Yes/No since we want to select the value for.
  • Default Value: You can select a value that will automatically populate for new products.
  • Unique Value: If “yes”, the data saved in this attribute has to be unique for each product. In this case, we’ll say “no”, because multiple featured can have the same featured.
  • Values Required: If “yes”, you will be required to select data in this attribute field when saving a product that uses it.Input Validation:
  • Apply To: This determines for which Product Types this attribute will display. For example, price wouldn’t make sense in a grouped product, as each Simple Product associated to the Grouped Product will have its own price. Therefore, you can set price to not apply to grouped product, and you won’t be scratching your head over what numbers to write in there.
  • Use in quick/advanced search: If “yes”, the data saved in this attribute will be searchable in the front-end in the quick search and advanced search, respectively.
  • Comparable on Front-end: If “yes”, this attribute will be displayed in a product comparison, so long as at least one product in the Compare List contains a value for this attribute.
  • Use in Layered Navigation: If “filterable”, this data will be available as a filter in Magento’s layered navigation. Filterable (with results) means that only values that are associated to specific products in a given category page will list in the Layered Navigation menu. Filterable (no results) means that all existing values for an attribute will display in the menu, even if using this as a filter will produce no results. There’s one condition to use attributes in layered navigation – the Input Type has to be Dropdown, Multiple Select, or Price.
  • Position: This determines the position of this attribute in the Layered Nav menu.
  • Visible on Catalog Pages on Front-end: If “no”, this attribute will not be displayed in the product info page. The “visibility” only controls whether or not the attribute displays in the front-end. Keep in mind – if you set an attribute to “Visible: No”, and leave it as “Comparable: Yes”, this attribute will still show up in comparison

Manage Label / Options

Picture5

Under this tab, you can enter the label for the attribute in the front-end. If you control stores in multiple languages, you can enter the label in each language under this tab. If a store’s label is blank, this store will look to the default label and use this one. Here we’ll enter “Featured” under “Default”.

Apply an attribute for attribute sets – Default

  1. Log into the Magento admin panel
  2. Navigate to “Manage Attributes” under Catalog -> Attributes -> Manage Attribute Sets
  3. Click on “Default” or any attribute sets.
  4. You’re now on the Attribute Set edit page and move Featured to Group General or any group. It looks like this:

Picture6

Applying to a product


Now, to apply this selectable attribute to a product, just add it to an attribute set and create a new product using this set. Below is a simplified example of the General tab of creating a new product in the admin:

Picture7

Similar to show new product on home page.


Step 1: In app/code/local/Mage/Catalog/Block/Product (if this folder does not exist, please create it). Create block file(Featured.php) that extend to Mage_Catalog_Block_Product_List.

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

In this following code, using addAttributeToFilter for filter attribute Featured and using addFieldToFilter for filter field SKU of product having sku value like “PROD0003”.

&lt;?php

class Mage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_List {

protected function _getProductCollection() {
 if (is_null($this-&gt;_productCollection)) {
 $collection = Mage::getResourceModel('catalog/product_collection')
 -&gt;addAttributeToSelect(Mage::getSingleton('catalog/config')-&gt;getProductAttributes())
 -&gt;addStoreFilter()
 -&gt;addPriceData()
 -&gt;addTaxPercents()
 -&gt;addUrlRewrite()
 -&gt;addAttributeToFilter('featured', array('eq' =&gt; 1))
 -&gt;addFieldToFilter('sku', array('like', 'PROD0003'))
 -&gt;setPageSize($this-&gt;get_prod_count())
 -&gt;setCurPage($this-&gt;get_cur_page());
 Mage::getSingleton('catalog/product_status')-&gt;addVisibleFilterToCollection($collection);
 Mage::getSingleton('catalog/product_visibility')-&gt;addVisibleInCatalogFilterToCollection($collection);
 $this-&gt;setProductCollection($collection);
 }
 return $collection;
 }

}
Step 2: In layout file(cms.xml) or any CMS Page add following code:
For layout file(cms.xml):
&lt;cms_index_index translate=&quot;label&quot;&gt;
 &lt;label&gt;CMS Home Page&lt;/label&gt;
 &lt;reference name=&quot;content&quot;&gt;
 &lt;block type=&quot;catalog/product_featured&quot; name=&quot;product_featured&quot; template=&quot;catalog/product/list.phtml&quot;&gt;
 &lt;block type=&quot;catalog/product_list_toolbar&quot; name=&quot;product_list_toolbar&quot; template=&quot;catalog/product/list/toolbar.phtml&quot;&gt;
 &lt;block type=&quot;page/html_pager&quot; name=&quot;product_list_toolbar_pager&quot;/&gt;
 &lt;/block&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;empty&lt;/layout&gt;&lt;count&gt;6&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;one_column&lt;/layout&gt;&lt;count&gt;5&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_left&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;two_columns_right&lt;/layout&gt;&lt;count&gt;4&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;addColumnCountLayoutDepend&quot;&gt;&lt;layout&gt;three_columns&lt;/layout&gt;&lt;count&gt;3&lt;/count&gt;&lt;/action&gt;
 &lt;action method=&quot;setToolbarBlockName&quot;&gt;&lt;name&gt;product_list_toolbar&lt;/name&gt;&lt;/action&gt;
 &lt;/block&gt;
&lt;/reference&gt;
 &lt;/cms_index_index&gt;

For any CMS page(e.g: Home Page):
Go to CMS->Pages, edit Home page like this:
Add following code to field Layout Update XML:

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

Picture8

Please share your impressions and ideas in comments below !

14638 Total Views 7 Views Today