0

As the title suggests, I am after a function that can display 30 recently viewed products by all users - possibly other than the active user.


The code below does it. I now just need to exclude the active user from the list - who might be a guest.

$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();

$this->_collection = Mage::getModel("reports/product_index_viewed")
                ->getCollection()
                ->addAttributeToSelect($attributes);


$this->_collection->excludeProductIds(Mage::getModel("reports/product_index_viewed")->getExcludeProductIds())
                    ->addUrlRewrite()
                    ->setPageSize(5)
                    ->setCurPage(1);

/* Price data is added to consider item stock status using price index */
$this->_collection->addPriceData();

$ids = $this->getProductIds();
if (empty($ids)) {
                $this->_collection->addIndexFilter();
} else {
                $this->_collection->addFilterByIds($ids);
}
$this->_collection->setAddedAtOrder();
if ($this-> _useProductIdsOrder && is_array($ids)) {
                $this->_collection->setSortIds($ids);
}

Mage::getSingleton('catalog/product_visibility')
                ->addVisibleInSiteFilterToCollection($this->_collection);
$_products = $this->_collection;

1 Answer 1

0

I ended up using DB query to perform this job.

$connection = Mage::getModel('core/resource')->getConnection('core_read');

$sql = 'SELECT * 
        FROM `report_viewed_product_index` 
        WHERE `store_id` = "' . Mage::app()->getStore()->getStoreId() . '"
        ' . (Mage::getSingleton('customer/session')->getCustomerId() !== null ? 'AND `customer_id` <> ' . Mage::getSingleton('customer/session')->getCustomerId() : '') . '
        ' . (Mage::getSingleton('log/visitor')->getId() !== null ? 'AND `visitor_id` <> ' . Mage::getSingleton('log/visitor')->getId() : '') . '
        ORDER BY `added_at` DESC 
        LIMIT 12';

$products = $connection->fetchAll($sql);

Why I did this? This is because when getModel("reports/product_index_viewed") Magento kept using products table; so getCollection() will provide you with a list of products. The ORM & luck of documentation wasn't an option - waste of time. Hence why the best solution was to utilise raw sql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.