Magento:如何更改此代码以获取最近查看过的产品?

时间:2014-01-13 21:05:12

标签: magento magento-1.7

我正在使用以下代码获取浏览量最高的产品:

<?php 
class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract{
    public function __construct() {
    parent::__construct();
    $storeId    = Mage::app()->getStore()->getId();
    $products = Mage::getResourceModel('reports/product_collection')
                    ->addOrderedQty()
                    ->addAttributeToSelect('*')
                    ->addAttributeToSelect(array('name', 'price', 'small_image'))
                    ->setStoreId($storeId)
                    ->addStoreFilter($storeId)
                    ->addViewsCount();
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

    $products->setPageSize(5)->setCurPage(1);
    $this->setProductCollection($products);
    }
}

为了获得最近观看的内容而不是最常查看的内容,可以对此代码进行哪些更改?我已经有了一个模板来展示它。对于大多数人来说,我都是this tutorial

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

“ - &gt; addViewsCount()”是过滤器的特殊配料。

请查看:http://blog.chapagain.com.np/magento-how-to-get-most-viewed-products/

这基本上是上面的,但添加了一个时间帧。

public function getMostViewedProducts()
{  
    // number of products to display
    $productCount = 5;

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

    // get today and last 30 days time
    $today = time();
    $last = $today - (60*60*24*30);

    $from = date("Y-m-d", $last);
    $to = date("Y-m-d", $today);

    // get most viewed products for last 30 days
    $products = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')     
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount()
        ->addViewsCount($from, $to)
        ->setPageSize($productCount);

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

    return $products;
}