【问题标题】:Magento: How to change this code to get the recently viewed products?Magento:如何更改此代码以获取最近查看的产品?
【发布时间】:2014-01-13 21:05:11
【问题描述】:

我正在使用下面的代码来获取浏览次数最多的产品:

<?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

感谢您的帮助!

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    "->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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多