cancel
Showing results for 
Search instead for 
Did you mean: 

product collection manually/programmatically

SOLVED

product collection manually/programmatically

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

2 REPLIES 2

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

Re: product collection manually/programmatically