1

On the site I'm currently working on, there is a tabbed area for new products and supposed to have a "most viewed" tab, but "most viewed" doesn't seem to appear - even after clearing caches - whilst new products does.

I've created a new module for it at /app/code/local/VendorX/MostViewed with the following files:

etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <VendorX_MostViewed>
            <version>1.0.0</version>
        </VendorX_MostViewed>
    </modules>
    <global>
        <blocks>
            <vendorx_mostviewed>
                <class>VendorX_MostViewed_Block_MostViewed</class>
            </vendorx_mostviewed>
        </blocks>
    </global>
</config>

Block/MostViewed.php

class VendorX_MostViewed_Block_MostViewed extends Mage_Core_Block_Template
{
    public function getMostViewedProducts()
    {
        $storeId = (int) Mage::app()->getStore()->getId();

        // Date
        $date = new Zend_Date();
        $toDate = $date->setDay(1)->getDate()->get('Y-MM-dd');
        $fromDate = $date->subMonth(1)->getDate()->get('Y-MM-dd');

        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addStoreFilter()
            ->addPriceData()
            ->addTaxPercents()
            ->addUrlRewrite()
            ->setPageSize(6);

        $collection->getSelect()
            ->joinLeft(
                array('aggregation' => $collection->getResource()->getTable('sales/bestsellers_aggregated_monthly')),
                "e.entity_id = aggregation.product_id AND aggregation.store_id={$storeId} AND aggregation.period BETWEEN '{$fromDate}' AND '{$toDate}'",
                array('SUM(aggregation.qty_ordered) AS sold_quantity')
            )
            ->group('e.entity_id')
            ->order(array('sold_quantity DESC', 'e.created_at'));

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

        return $collection;
    }
}

app/design/frontend/vendorx/website/layout/local.xml

...
<block type="core/template" name="featured_tabs_2" template="catalog/product/list/featured/tabbed.phtml" after="-">
                <action method="setTitle"><title><![CDATA[<span class="black">Best Selling</span> Clubs]]></title></action>
                <block type="catalog/product_new" name="new-products" as="new-products" template="catalog/product/list/featured.phtml">
                    <action method="setHeading"><heading>Just Arrived</heading></action>
                    <action method="setCacheLifetime"><lifetime>86400</lifetime></action>
                    <action method="setCacheKey"><key>hp_new_prods</key></action>
                </block>
                <block type="vendorx_mostviewed/mostviewed" name="most-viewed" as="most-viewed" template="catalog/product/list/featured.phtml">
                    <action method="setHeading"><heading>Most Viewed</heading></action>
                    <action method="setCacheLifetime"><lifetime>86400</lifetime></action>
                    <action method="setCacheKey"><key>hp_most_viewed</key></action>
                </block>
            </block>
...

I have run through xdebug and for some reason it is not going in to the mostviewed class (as defined on the block type) and when I change the block type to catalog/product_new I get some products on the front end showing (albeit the same as the new-products tab block.

Any help would be massively appreciated.

1

In your config.xml it should be

    <blocks>
        <vendorx_mostviewed>
            <class>VendorX_MostViewed_Block</class>
        </vendorx_mostviewed>
    </blocks>

instead of

    <blocks>
        <vendorx_mostviewed>
            <class>VendorX_MostViewed_Block_MostViewed</class>
        </vendorx_mostviewed>
    </blocks>

Although the element name class might suggest otherwise, this is not a class name, but a prefix that is used for all block aliases starting with vendorx_mostviewed/

Also, the block alias for VendorX_MostViewed_Block_MostViewed will be vendorx_mostviewed/mostViewed (notice the capitalization). The first character of each underscore separated word is lower cased in the alias, but inner camel case must be preserved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.