I need to save the recently viewed products in a cookie. The way that if there's no recently viewed products in session it reads it from the cookie. But I can't find where to find this exactly.
I'm overriding the file app/code/core/Mage/Reports/Block/Product/Viewed.php in my local, where I added the method getItemsCollection() from Mage_Reports_Block_Product_Abstract, but my code doesn't work.
It saves the ids in a cookie, but I can't load them later.
public function getItemsCollection()
{
if (is_null($this->_collection)) {
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$this->_collection = $this->_getModel()
->getCollection()
->addAttributeToSelect($attributes);
if ($this->getCustomerId()) {
$this->_collection->setCustomerId($this->getCustomerId());
}
$this->_collection->excludeProductIds($this->_getModel()->getExcludeProductIds())
->addUrlRewrite()
->setPageSize($this->getPageSize())
->setCurPage(1);
$recent_viewed_product_ids = $this->_collection->getAllIds();
$cookie = Mage::getSingleton('core/cookie');
if ($cookie->get('recent_products')) {
$cookie_ids = unserialize($cookie->get('recent_products'));
$recent_viewed_product_ids = array_unique(array_merge($recent_viewed_product_ids,$cookie_ids), SORT_REGULAR);
}
if (!empty($recent_viewed_product_ids)) {
$cookie = Mage::getSingleton('core/cookie');
$cookie->set('recent_products', serialize($recent_viewed_product_ids), strtotime('2030-31-12 23:59:59 GMT'), '/');
$this->_collection = $this->_getModel()
->getCollection()
->addAttributeToSelect($attributes)
->addAttributeToFilter('entity_id', array('in' => $recent_viewed_product_ids));
if ($this->getCustomerId()) {
$this->_collection->setCustomerId($this->getCustomerId());
}
$this->_collection
->setPageSize($this->getPageSize())
->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);
}
return $this->_collection;
}