How to display products from a category in a CMS static block

There might have been a situation when you would have created a special category like “Sale Products” or “Featured Products”… and you had wanted to display products from these categories on some page like Home Pages using the CMS static block.

In order to achieve this you may create a CMS static block and add the following segment of code:


{{block type="catalog/product_list" name="product_list" category_id="2" column_count="4" template="catalog/product/list.phtml" }}

This will display the products listing page as a block on the page.

In case you want to change the page structure like you will want to remove the toolbars from the product listing then you can copy catalog/product/list.phtml to a new file catalog/product/custom_list.phtml and modify it as per your need. Next you need to call the static block as follows:


{{block type="catalog/product_list" name="product_list" category_id="2" column_count="4" template="catalog/product/custom_list.phtml" }}

The numbers of products displayed is the same number as the product listing page and is pulled from the Admin >> System >> Configuration >> Catalog >> Frontend > Products per Page on Grid Default Value/Products per Page on List Default Value.

When we are displaying products in a block we will generally want to have a separate control over the number of products displayed.

So the first option that come to our mind is to create our own block and load the product collection. You need not do this.

In the block class Mage_Catalog_Block_Product_List_beforeToHtml” method, product collection is passed to the toolbar object.


protected function _beforeToHtml()
{
$toolbar = $this->getToolbarBlock();
.
.
.

// set collection to toolbar and apply sort
$toolbar->setCollection($collection);

The product limit is calculated in the class Mage_Catalog_Block_Product_List_ToolbarsetCollection” method.


public function setCollection($collection)
{
$this->_collection = $collection;

$this->_collection->setCurPage($this->getCurrentPage());

// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {su
$this->_collection->setPageSize($limit);
}

public function getLimit()
{
$limit = $this->_getData('_current_limit');

Thus we need to set the $toolbar object value “_current_limit” for setting the product limit.

We will create a custom Magento module – Techcategory

First we need to create the module registration file Techawaken_Techcategory.xml in {{MAGENTO_ROOT}}/app/etc/modules folder with the following contents:


<?xml version="1.0"?>
<config>
<modules>
<Techawaken_Techcategory>
<active>true</active>
<codePool>local</codePool>
<depends><Mage_Catalog/></depends>
</Techawaken_Techcategory>
</modules>
</config>

Next step is to create the module configuration file config.xml in the folder {{MAGENTO_ROOT}}/ app/code/local/Techawaken/Techcategory/etc with the following contents:


<?xml version="1.0"?>
<config>
<modules>
<Techawaken_Techcategory>
<version>0.0.1</version>
</Techawaken_Techcategory>
</modules>
<global>
<blocks>
<techcategory>
<class>Techawaken_Techcategory_Block</class>
</techcategory>
</blocks>
</global>
</config>

Now finally we will create the block class file {{MAGENTO_ROOT}}/app/code/local/Techawaken/Techcategory/Block/Product/List.php


<?php

class Techawaken_Techcategory_Block_Product_List extends Mage_Catalog_Block_Product_List
{
/**
* Retrieve Toolbar block
*
* @return Mage_Catalog_Block_Product_List_Toolbar
*/
public function getToolbarBlock()
{
if ($blockName = $this->getToolbarBlockName()) {
if ($block = $this->getLayout()->getBlock($blockName)) {
return $block;
}
}
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());

if ($this->getProductLimit()) {
$block->setData('_current_limit', $this->getProductLimit());
}

return $block;
}

}

 

Here’s the code that you will now add to a CMS Static block to display 8 products:


{{block type="techcategory/product_list" name="featured_product_list" category_id="2" column_count="4" product_limit="8" section_title="Featured Products" template="catalog/product/custom_list.phtml" }}

“product_limit” :  is the parameter to set the number of products to be displayed.

In the phtml file “catalog/product/custom_list.phtml” if you call


echo $this->getSectionTitle();

it will display the heading “Featured Products” set in the CMS Static Block.

 

If you want a readymade solution then you can download Magento module ProxiBlue/CatalogListFilters from GitHub and install it. It works on event/observer model.

The code to be inserted in CMS Static block is:


{{block type="catalog/product_list" category_id="2" product_limit="5" template="catalog/product/custom_list.phtml"}}

Leave a Comment

Back to top