4
votes

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:

Array
(
    [0] => 1965
    [1] => 3371
    [2] => 1052
)

I can then make this into a collection:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addIdFilter($product_ids);

Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:

$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);

And then I've tried to load the HTML of the results as follows:

$CollectionHTML = $ProductList->_toHtml();

But $CollectionHTML is empty.

How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?

1
Why are you bypassing the LAYOUT layer (xml layout files) ? In magento you shouldn't instantiate directly a PHP Block class .. you have to load it via $this->getLayout()->createBlock('catalog/product_list') or let the Layout file do it (it's their purpose). I suggest you to read/learn magento tutorials/booksJscti
@Bixi I agree with the separation of concerns, and I agree with you in principle, and thanks for raising that point, because other users should no that this isn't the ideal solution. But I don't want a philosophical debate here. I have to maintain a script and doing things your way (the right way) would involve a large refactor. Thus my question.Darth Egregious

1 Answers

3
votes

Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.

Anyway, back to your issue.

In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.

But if you want to create a block a way to do it is:

$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')

Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.

Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:

//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');  
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object 
echo $block->renderView();  

Displaying specific ids:

  • you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
  • you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)

But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)