Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Anyway, back to your issue.</p> <p>In Magento block are not instantiated like in any app <code>$myvar = new className</code> ... almost never. <a href="http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates" rel="nofollow">This tutorial</a> can help you understand better Magento's layout and blocks.</p> <p>But if you want to create a block a way to do it is: </p> <pre><code>$block = Mage::getSingleton('core/layout')-&gt;createBlock('catalog/product_list') </code></pre> <p>Now related to your product collection you should check how <code>Mage_Catalog_Block_Product_List::_getProductCollection</code> actually works, because it uses the layered navigation, not a simple product collection.</p> <p>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:</p> <pre><code>//$category_id needs to be set $layout = Mage::getSingleton('core/layout'); $toolbar = $layout-&gt;createBlock('catalog/product_list_toolbar'); $block = $layout-&gt;createBlock('catalog/product_list'); $block-&gt;setChild('toolbar', $toolbar); $block-&gt;setCategoryId($category_id); $block-&gt;setTemplate('catalog/product/list.phtml'); $collection = $block-&gt;getLoadedProductCollection(); $toolbar-&gt;setCollection($collection); //render block object echo $block-&gt;renderView(); </code></pre> <p>Displaying specific ids:</p> <ul> <li>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)</li> <li>you can hook into <code>catalog_block_product_list_collection</code> event to add your ID Filter to the collection (this is called in <code>_beforeToHtml</code> function)</li> </ul> <p>But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload