2012-02-07 19 views
7

Magentoのカテゴリ一覧ページで表示回数を表示したいと考えています。このデータは、reports/product_collectionを介してアクセスできるように見えますが、正しくアクセスする方法が見つかりません。magento製品のビュー数をproduct_idに基づいて取得

私は基本的には製品IDを提供して、その製品のビュー数を私に返します。

+3

私はこのスレッドに出会いましたhttp://www.magentocommerce.com/boards/viewthread/27684/? – Ledgemonkey

+0

これはうまくいきましたが、スレッドのようにかなり遅かったです。助けてくれてありがとう! – matt

答えて

15

Mage_Reports_Model_Resource_Product_Collectionモデルで表示回数を取得できます。あなたがカウントを取得するために$product->getViews()を使用できるように

// 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); 

コレクション内の各項目は、ビューのプロパティが設定されたcatalog/productインスタンスです。

あなたが製品全体のモデルをロードし、専用のビュー数を必要としない場合、あなたはこのようにそれを得ることができます。

$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です値はビュー数です。

+0

完璧!ご協力ありがとうございました。 – matt

+0

'SELECT COUNT(*)ASビューfrom report_viewed_product_index WHERE product_id = 446'を使用した差分は何ですか? –

+0

ここで、report_eventテーブルのobject_idとsubject_idの意味は何ですか? –

関連する問題