How to Display custom product collection at Magento frontend

magento

Lets say we want to display our custom product collection at our custom controller/action inside our custom Magento module,
Please check below example to get product collection :-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to Display custom product collection at Magento frontend</title>
</head>
<body>
	<?xml version="1.0" encoding="UTF-8"?>
 
        <layout version="0.1.0">
         
            <mymodule_productlist_index_index>
                <!-- Set title -->
                <reference name="head">
                    <action method="setTitle">
                        <title>Mmartinovic Productlist</title>
                    </action>
                </reference>
         
                <!-- Switch root template to 1 column -->
                <reference name="root">
                    <action method="setTemplate">
                        <template>page/1column.phtml</template>
                    </action>
                </reference>
         
                <reference name="content">
                    <!-- Add product list to content -->
                    <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
                        <!-- Add toolbar to product list -->
                        <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                            <!-- Add pager to toolbar -->
                            <block type="page/html_pager" name="product_list_toolbar_pager"/>
                        </block>
         
                        <!-- Specify toolbar block name -->
                        <action method="setToolbarBlockName">
                            <name>product_list_toolbar</name>
                        </action>
         
                        <!-- Use custom product collection -->
                        <action method="setCollection">
                            <value helper="mymodule_productlist/getProductCollection" />
                        </action>
         
                        <!-- Use custom available sort by orders -->
                        <action method="setAvailableOrders">
                            <value helper="mymodule_productlist/getAvailableOrders" />
                        </action>
         
                        <!-- Set the default sort by order -->
                        <action method="setSortBy">
                            <value>price</value>
                        </action>
         
                        <!-- Set default direction to ascending -->
                        <action method="setDefaultDirection">
                            <value>asc</value>
                        </action>
         
                        <action method="setColumnCount">
                            <coulumn>5</coulumn>
                        </action>
                    </block>
                </reference>
            </mymodule_productlist_index_index>
         
        </layout>

</body>
</html>

From above code we have changed layout to 1 column, filled content with catalog/product_list type block and added catalog/product_list_toolbar with page/html_pager inside.
Now if you have noticed that Mage_Catalog_Block_Product_List code, you will notice that Mage_Catalog_Block_Product_List “listens” for certain class properties when adjusting Mage_Catalog_Block_Product_List_Toolbar instance:


<?php
class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract
{
    protected function _beforeToHtml()
    {
        $toolbar = $this->getToolbarBlock();
        $collection = $this->_getProductCollection();
        if ($orders = $this->getAvailableOrders()) {
            $toolbar->setAvailableOrders($orders);
        }
        if ($sort = $this->getSortBy()) {
            $toolbar->setDefaultOrder($sort);
        }
        if ($dir = $this->getDefaultDirection()) {
            $toolbar->setDefaultDirection($dir);
        }
        if ($modes = $this->getModes()) {
            $toolbar->setModes($modes);
        }
        $toolbar->setCollection($collection);
        $this->setChild('toolbar', $toolbar);
        Mage::dispatchEvent('catalog_block_product_list_collection', array(
            'collection' => $this->_getProductCollection()
        ));
        $this->_getProductCollection()->load();
        return parent::_beforeToHtml();
    }
}
?>

Please check Below Helper code:-

 

<?php
 
class Mmartinovic_Productlist_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function getAvailableOrders()
    {
        return array(
            'price' => $this->__('Price')
        );
    }
 
    public function getProductCollection()
    {
        $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
 
        $collection = Mage::getModel('catalog/category')
            ->load($rootCategoryId)
            ->getProductCollection()
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite($rootCategoryId);
 
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
 
        return $collection;
    }
 
}
?>
Posted in Uncategorized