Magento增加评论次数显示Get view count for magento product based on product_id

2,246 人次阅读
没有评论

共计 1829 个字符,预计需要花费 5 分钟才能阅读完成。

http://stackoverflow.com/questions/9172755/get-view-count-for-magento-product-based-on-product-id —-暂时不可用

http://www.magentocommerce.com/boards/viewthread/27684/ —可用

1.
[php]
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);

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);
This gives you an array, the keys are the product ids and the values are the view counts.

[/php]

2.
[php] <?php
$srt_proooion_id = $_product->getId();
$int_proooion_id = (int)$srt_proooion_id;

$db = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);
$result = $db->query("select count(*) as ‘0’ from report_event where `object_id`= $int_proooion_id");

$str_view_fores = $result->fetch(PDO::FETCH_ASSOC);
$int_fores = (int)$str_view_fores[0] ;

echo ‘<p align="right"> Views of product: <b> ‘.$int_fores.'</b> times</p>’;
?>[/php]

正文完
 0