0

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;
}
0

I think you have to mistakes in your code:

First, I guess getAllIds() of the collection returns more than just the last viewed ids since this method removes all joins, resets limits and filters etc.

So instead of $recent_viewed_product_ids = $this->_collection->getAllIds(); you should rather use $recent_viewed_product_ids = $this->getProductIds();

The second issue is ->addAttributeToFilter('entity_id', array('in' => $recent_viewed_product_ids));. entity_id is not an attribute therefore addFieldToFilter is the correct method. But anyway the collection has a method addFilterByIds so you can just call addFilterByIds($recent_viewed_product_ids); on the collection.

And you have some copied code which changes the collection again after your cookie logic.

I guess, something like this should work for you:

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

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

        $recent_viewed_product_ids = $this->getProductIds();

        $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->addFilterByIds($recent_viewed_product_ids);
        } else {
            $this->_collection->addIndexFilter();
        }

        $this->_collection->setAddedAtOrder();
        if ($this->_useProductIdsOrder && is_array($recent_viewed_product_ids)) {
            $this->_collection->setSortIds($recent_viewed_product_ids);
        }

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

    return $this->_collection;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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