0

In my catalog/product_list block template (catalog/product/list.phtml), I've this piece of code that should work, but $_productCollection is always false:

<?php
$session = Mage::getSingleton('core/session');
$_productCollection = $session->getProductCollection();

if (!$_productCollection){

    $_productCollection=$this->getLoadedProductCollection();
    $session->setProductCollection($_productCollection);
}

$_helper = $this->helper('catalog/output');

Strings work, just not collection objects it seems. In my IDE I can see the $_SESSION superglobal populating with the collection, but on refresh it just disappears. Maybe it is garbage collected when the request completes? Any one have any idea?

Note, this page is not using a toolbar.

2 Answers 2

4

When handling objects and session, you need to serialize / unserialize data :

<?php
$session = Mage::getSingleton('core/session');
$_productCollection = unserialize($session->getProductCollection());

if (!$_productCollection){

    $_productCollection=$this->getLoadedProductCollection();
    $session->setProductCollection(serialize($_productCollection));
}
1
  • 1
    This is actually good answer wrt to collection try to save in session. I was facing pagination issue after saving collection in session without serializing. Do serialize for further operations on collection/object. Jan 10, 2018 at 13:13
3

Maybe you can use cache instead of session to solve your issue, but since some info is missing it might not be the best approach. Same piece of code with cache should look like this:

<?php
$cache = Mage::app()->getCache();
$_productCollection = $cache->load("product_collection_cache");

if (!$_productCollection){
    $_productCollection = $this->getLoadedProductCollection();
    $cache->save(date("r"), "product_collection_cache", $_productCollection, 10);
}

A good idea if the key should be tied to a customer is to change the cache key to something like product_collection_cache_{customer_id}

0

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.