根据product_id获取magento产品的观看次数 [英] Get view count for magento product based on product_id
问题描述
我想在Magento的类别列表页面上显示观看次数.这些数据看起来曾经可以通过report/product_collection访问,但我找不到正确访问它的方法.
I'd like to display a view count on a category listing page in Magento. This data looks like it used to be accessible via reports/product_collection but I can't find a way to access it correctly.
我基本上想提供一个产品ID,并获得退还给我的该产品的观看次数.
I'd basically like to supply a product id and get the view count of the said product returned to me.
推荐答案
您可以通过Mage_Reports_Model_Resource_Product_Collection
模型获取视图计数.
You can get the view count through the Mage_Reports_Model_Resource_Product_Collection
model.
// set $to and $from to an empty string to disable time range filtering
$from = '2012-01-01';
$to = now();
$productIds = array(9, 35); // your product ids, (works as an int, too)
$reports = Mage::getResourceModel('reports/product_collection')
->addViewsCount($from, $to)
->addFieldToFilter('entity_id', $productIds);
集合中的每个项目都是一个设置了views属性的catalog/product
实例,因此您可以使用$product->getViews()
来获取计数.
Each item in the collection is a catalog/product
instance with a views property set, so you can use $product->getViews()
to get the count.
如果您不想加载整个产品模型,而只需要视图计数,则可以这样获得:
If you don't want to load the whole product model and only require the view count, you can get it like this:
$resource = Mage::getResourceModel('reports/event');
$select = $resource->getReadConnection()->select()
->from(array('ev' => $resource->getMainTable()), array(
'product_id' => 'object_id',
'view_count' => new Zend_Db_Expr('COUNT(*)')
))
// join for the event type id of catalog_product_view
->join(
array('et' => $resource->getTable('reports/event_type')),
"ev.event_type_id=et.event_type_id AND et.event_name='catalog_product_view'",
''
)
->group('ev.object_id')
// add required filters
->where('ev.object_id IN(?)', productIds)
->where('ev.logged_at >= ?', $from)
->where('ev.logged_at <= ?', $to);
$result = $resource->getReadConnection()->fetchPairs($select);
这为您提供了一个数组,键是产品ID,值是视图计数.
This gives you an array, the keys are the product ids and the values are the view counts.
这篇关于根据product_id获取magento产品的观看次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!