Showing results for 
Search instead for 
Did you mean: 

product collection manually/programmatically


product collection manually/programmatically



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 )


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


Tags (1)

Re: product collection manually/programmatically

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

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.

Re: product collection manually/programmatically