Magento category bestseller

The first subject I tackle is the bestseller. I will extend the exemple provided by inchoo net for the magento product bestseller. First download the code.

Modify the following lines from bestseller.phtml

$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addAttributeToFilter('visibility', $visibility)
->setOrder('ordered_qty', 'desc');

into

$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addAttributeToFilter('visibility', $visibility);
if ($current_category = Mage::registry('current_category'))
$_productCollection->addCategoryFilter($current_category);
$_productCollection->setOrder('ordered_qty', 'desc');

With the lines bellow we simply aply an extra current category filter for the database query.

if ($current_category = Mage::registry(‘current_category’))
$_productCollection->addCategoryFilter($current_category);

What we need still to do is to modify catalog.xml of our template (in my case it’s app/design/frontend/default/default/layout/catalog.xml) adding our bestseller block to the produc list blocks <block type=”core/template” name=”bestseller” template=”bestseller/bestseller.phtml”/>. The xml layout for category view will look something like this.

    <catalog_category_default translate="label">
    <label>Catalog Category (Non-Anchor)</label>
    <reference name="left">
    </reference>
    <reference name="content">
    <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
    <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
 
    <block type="core/template" name="bestseller" template="bestseller/bestseller.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>
    </block>
    </reference>
    </catalog_category_default>

Or better add it to your theme’s local.xml file if your magento version supports it.
The last modifications you will need to do call the bestseller child block from his parent product list block like this:

<?php echo $this->getChildHtml(‘bestseller’); ?>

If everything goes well you should have something like this  http://www.mage-contacts.com/magento-1.4.2-sample/electronics/cameras.html .

You can download Magento category bestseller and modify it for your suit.
Magento category bestseller is sligthly different from the example given, it will run correctly also on a magento with multiple websites and stores. Please don’t override your themes local.xml, catalog.xml and list.phtml files because these are provided as an example too see what modifications you have to do.

This entry was posted in bestseller and tagged , , , , , . Bookmark the permalink.

6 Responses to Magento category bestseller

  1. Roman Klein says:

    Hi Erik,
    your extended code looks great, but adding to the cart doesn’t work and there is no price shown. Any tips how to manage these issues?

  2. erik says:

    In the meantime the issue regarding the adding in the cart and price display has been solved. I have uploaded a new bestseller.phtml to the zip file.
    Also at inchoo the take on Tejasvini is valid.

    $storeId = Mage::app()->getStore()->getId(); $_product= Mage::getModel(‘catalog/product’)->setStoreId($storeId)->load($product->entity_id);
    $theProductBlock = new Mage_Catalog_Block_Product; echo $theProductBlock->getPriceHtml($_product, true);

    Thank you for bringing up the bug!

  3. Roman Klein says:

    Hi Erik,
    I have one more issue / idea which I cannot solve myself. How to modify the code for the block being affected by layered navigation filters?

    For example – I want to choose red backpack from selected category so I use filter for color. But the block with bestsellers is unchanged with different colors. It will be great for the customer if the content of the block also displays red backpacks.

    Do you have any hints?
    Thank you

  4. erik says:

    Hi Roman,

    Rapidly in case of layered navigation I would do the followings:
    Let’s take a single attribute: color. The above I would modify like this in the case of bestseller.phtml

    ///
    $color = $this->getRequest()->getParam(‘color’,0)

    $_productCollection = Mage::getResourceModel(‘reports/product_collection’)
    ->addAttributeToSelect(‘*’)
    ->addOrderedQty();
    if ($color)
    $_productCollection->addAttributeToFilter(‘color’, $color);
    $_productCollection->addAttributeToFilter(‘visibility’, $visibility);
    if ($current_category = Mage::registry(‘current_category’))
    $_productCollection->addCategoryFilter($current_category);
    $_productCollection->setOrder(‘ordered_qty’, ‘desc’);

    Even more the code can be extended to all the attributes used in layered navigation, get all the active filters, iterate them, get their values and apply them to the collection.
    But if you prefer simplicity you can taylor it just like:

    $color = $this->getRequest()->getParam(‘color’,0)
    $manufacturer = $this->getRequest()->getParam(‘manufacture’,0)

    .
    .
    .

    • Roman Klein says:

      Thank you for your help.

      Unfortunately I’m not able to filter the color even if I specify it directly:

      $_productCollection = Mage::getResourceModel(‘reports/product_collection’)
      ->addAttributeToSelect(‘*’)
      ->setStoreId($storeId)->addStoreFilter($storeId)
      ->addOrderedQty()
      ->setOrder(‘ordered_qty’, ‘desc’)
      ->addAttributeToFilter(‘color’,33)
      ->addAttributeToFilter(‘visibility’, $visibility)
      ->setPage(1,$totalPerPage);

      This code returns empty collection but should return at least 3 products. I have tried different variations but with no progress.

      The color attribute is set to be Used in Product Listing and the catalog is reindexed. I’m not using flat catalog.

      Any idea what am I doing wrong?

      • erik says:

        Hi Roman,

        I presume you also disabled cache while you tested and also that your color is simple select. Is you color attribute a global one? Do you have multiple storeviews?
        I’ve made the test on magento 1.4.2
        http://www.mage-contacts.com/magento-1.4.2-sample/electronics/cameras.html and it’s working for me.

        My code is the following:

        $storeId = Mage::app()->getStore()->getId();

        $totalPerPage = ($this->show_total) ? $this->show_total :3;
        $counter = 1;
        $visibility = array(
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
        );

        $color = $this->getRequest()->getParam(‘color’,0);

        $storeId = Mage::app()->getStore()->getId();
        $_productCollection = Mage::getResourceModel(‘reports/product_collection’)
        ->addAttributeToSelect(‘*’)
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addOrderedQty()
        ->addAttributeToFilter(‘visibility’, $visibility);

        if ($color)
        $_productCollection->addAttributeToFilter(‘color’, $color);

        if ($current_category = Mage::registry(‘current_category’))
        $_productCollection->addCategoryFilter($current_category);
        $_productCollection->setPage(1,$totalPerPage);
        $_productCollection->setOrder(‘ordered_qty’, ‘desc’);

        Mage::getSingleton(‘catalog/product_status’)->addVisibleFilterToCollection($_productCollection);
        Mage::getSingleton(‘catalog/product_visibility’)
        ->addVisibleInCatalogFilterToCollection($_productCollection);

Leave a Reply

Your email address will not be published. Required fields are marked *


× eight = 16

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>