Hi,
I want to create my own collection from product ids and use it in catalog/new.phtml. I have changed this:
$_products = $this->getProductCollection()
for this:
$_products = getCustomProductCollection( $items )
where:
function getCustomProductCollection( $_items ) { $collection = new Varien_Data_Collection(); if ( $_items ) { foreach($_items as $id ) { $varienObject = new Varien_Object(); $varienObject->setData( Mage::getModel('catalog/product')->load($id) ); $collection->addItem($varienObject); } } return $collection; }
I tried this but i get "PHP Fatal error: Call to a member function roundPrice() on a non-object".
Is there a correct way to do this?
Solved! Go to Solution.
In your case, the expected object from the items array is the class Mage_Catalog_Model_Product exemplar. At the same time, the result Varien_Object contains the Mage_Catalog_Model_Product class. Try to modify your code in the following way:
foreach($_items as $id ) { $collection->addItem(Mage::getModel('catalog/product')->load($id)); }
Actually, it is not a good idea to modify the collection and load the module inside the template. If you manage to describe your situation in details, we'll try to provide a solution for you.
In your case, the expected object from the items array is the class Mage_Catalog_Model_Product exemplar. At the same time, the result Varien_Object contains the Mage_Catalog_Model_Product class. Try to modify your code in the following way:
foreach($_items as $id ) { $collection->addItem(Mage::getModel('catalog/product')->load($id)); }
Actually, it is not a good idea to modify the collection and load the module inside the template. If you manage to describe your situation in details, we'll try to provide a solution for you.