Question

I am displaying bestsellers on a blank layout right now, but I want to use the same layout and template as in product catalog, to be able to switch between grid and layout mode, apply filters, etc. Is there anyting I can do to display it this way?

Fetching bestsellers:

public function getBestsellersCollection() {
    $productCount = 5;
    $storeId = Mage::app()->getStore()->getId();

    $productsBestSellerMens = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->setStoreId($storeId)
            ->setOrder('ordered_qty', 'desc');

    return $productsBestSellerMens->getData();
}

config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Dexm_Best>
        <version>1.0.0</version>
    </Dexm_Best>
</modules>
<global>
    <helpers>
        <best>
            <class>Dexm_Best_Helper</class>
        </best>
    </helpers>
</global>
<frontend>
    <routers>
        <best>
            <use>standard</use>
            <args>
                <module>Dexm_Best</module>
                <frontName>bestsellers</frontName>
            </args>
        </best>
    </routers> 
    <layout>
    <updates>
        <best>
            <file>dexm_best.xml</file>
        </best>
    </updates>
</layout> 
</frontend>
</config>

dexm_best.xml

<best_index_index>
    <reference name="content">
        <block type="core/template" name="best" template="dexm/best/bestsellers.phtml" />
    </reference>
</best_index_index>  

bestsellers.phtml

$product = Mage::helper('best')->getBestsellersCollection();
Was it helpful?

Solution

app/etc/modules/Dexm_Best.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Dexm_Best>
            <active>true</active>
            <codePool>local</codePool>
        </Dexm_Best>
    </modules>
</config>

app/local/Dexm/Best/etc/config.xml

<?xml version="1.0"?>
<config>    
    <modules>
        <Dexm_Best>
            <version>1.0.0.0.1</version>
        </Dexm_Best>
    </modules>
    <global>
        <helpers>
            <best>
                <class>Dexm_Best_Helper</class>
            </best>
        </helpers>
        <blocks>
            <best>
                <class>Dexm_Best_Block</class>
            </best>
        </blocks>
    </global>
    <frontend>
        <routers>
            <best>
                <use>standard</use>
                <args>
                    <module>Dexm_Best</module>
                    <frontName>bestsellers</frontName>
                </args>
            </best>
        </routers>
        <layout>
            <updates>
                <best>
                    <file>dexm_best.xml</file>
                </best>
            </updates>
        </layout> 
    </frontend>
</config>

app/etc/local/Dexm/Best/controllers/IndexController.php

Class Dexm_Best_IndexController extends Mage_Core_Controller_Front_Action {


    public function indexAction() {


        $this->loadLayout();
        $this->renderLayout();
    }
}

app/code/local/Dexm/Best/Block/Bestseller.php

class Dexm_Best_Block_Bestseller extends Mage_Catalog_Block_Product_List {

    protected function _getProductCollection()
    {
        if (is_null($this->_productCollection)) {


            $storeId = Mage::app()->getStore()->getId();
            $collection = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->setStoreId($storeId)
            ->setOrder('ordered_qty', 'desc');

            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
            $this->_productCollection = $collection;

        }
        return $this->_productCollection;
    }

}

app/design/frontend/rwd/default/layout/dexm_best.xml

<?xml version="1.0"?>
    <layout version="0.1.0">        
    <best_index_index translate="label">
        <label>Best Seller Product</label>
        <reference name="head">
            <action method="setTitle" translate="title" module="best"><title>Best Seller Product</title></action>
        </reference>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
            <action method="setHeaderTitle" translate="title" module="best"><title>Best Seller Product</title></action>
        </reference>
        <reference name="content">      
            <block type="best/bestseller" name="product_list" 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>
                <action method="setColumnCount"><columns>3</columns></action>
            </block>
        </reference>
    </best_index_index> 
</layout>

Screenshot : enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top