加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 根据product_id获取magento产品的查看次数

发布时间:2020-12-13 18:27:15 所属栏目:PHP教程 来源:网络整理
导读:我想在Magento的类别列表页面上显示查看次数.这些数据看起来像以前可以通过reports / product_collection访问,但我找不到正确访问它的方法. 我基本上想提供一个产品ID并将所述产品的查看次数归还给我. 您可以通过Mage_Reports_Model_Resource_Product_Collec
我想在Magento的类别列表页面上显示查看次数.这些数据看起来像以前可以通过reports / product_collection访问,但我找不到正确访问它的方法.

我基本上想提供一个产品ID并将所述产品的查看次数归还给我.

您可以通过Mage_Reports_Model_Resource_Product_Collection模型获取视图计数.
// 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属性集的目录/产品实例,因此您可以使用$product-> getViews()来获取计数.

如果您不想加载整个产品模型并且只需要查看计数,则可以这样:

$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,值是视图计数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读