0

I am planning to show a product list on the front page that can be sorted by category with a drop-down box.

Previously I was loading my products with the static category ids in a CMS-Page and display/hide it if the user was selecting a category.

My problem is that this solution is not really dynamic. If I add a new product category, then I need to edit the CMS Page and add a hidden <div /> with the product grid on it.

The product grid is generated by the below code:

{{block type="catalog/product_list" category_id="16" template="catalog/product/list.phtml"}}             )

I want to generate this divs dynamically in my frontpage template. I tried this:

$_layout  = Mage::getSingleton('catalog/product_list');
$_block   = $_layout->createBlock('catalog/product_list')->setTemplate('catalog/product/list.phtml');

echo $_block->toHtml();

But it doesn't work. I need a loop through all categories with their ids(i already have this part) to generate a hidden grid of the specific products in it.

0

It is better if you can create a custom template to do all these logic. You can include your custom template in a CMS Page like this:

{{block type="core/template" template="your/custom/template.phtml"}}

Now in your template, you can try this code:

<?php
//your category collection should come here.
//Assumes $categories holds all categories.
$categories;

$blocks = [];
$layout = $this->getLayout();
foreach ($categories as $category) {
    $blocks[$category->getId()] = $layout->createBlock('catalog/product_list')
        ->setCategoryId($category->getId());
}
?>

<?php
//now $blocks contain a list block for all categories.
foreach ($blocks as $categoryId => $block) {
?>

    <div><?php echo $block->toHtml() ?></div>
<?php } ?>

All you need to do here is, add your category collection logic in this template and fill $categories with those categories. We are creating blocks for each of those categories and then finally loop through those blocks and print its content inside a <div>.

Again adding all big logic in a template file is a BIG NO NO. The correct way is you should add your own custom block and move all this logic to your custom block and thus keep your template clean and easy to read.

Finally, if you are working in Magento 1.9 or greater version, then you need to whitelist core/template block through admin side. If you are using a custom block, then you need to whitelist that too.

Hope that will lead you in a proper direction.

EDIT

I just tried out this now. Following code seems to be working.

<?php
$productListBlocks = array();
$layout = $this->getLayout();

//loading category; replace your logic here.
$categoryIds = [15, 19];
$categories = Mage::getModel('catalog/category')->getCollection();
$categories->addIdFilter($categoryIds);

//creating product list block corresponds to categories.
foreach ($categories as $category) {
    $productListBlocks[] = $layout->createBlock('catalog/product_list')
        ->setCategoryId($category->getId())
        ->setTemplate('catalog/product/list.phtml');
}
?>

<!-- RENDER PRODUCT LIST HERE -->
<?php foreach ($productListBlocks as $productListBlock) { ?>
    <div><?php echo $productListBlock->toHtml();; ?></div>
<?php } ?>

So as you can see, the mistake that we have in the first code is that we forgot to set the template to product list block. With that in place, I can see the list is showing up.

6
  • Thanks for your help. I added a custom template in my CMS Page and added your Code there. I modified it a bit to only load the category id 16(which is added in my admin panel and contains products). But it dont show any output. The template itself is whitlisted executed fine. Here is my Code:
    – J. Doe
    Sep 29 '17 at 8:48
  • <?php //your category collection should come here. //Assumes $categories holds all categories. $categories; $blocks = []; $layout = $this->getLayout(); $blocks[0] = $layout->createBlock('catalog/product_list') ->setCategoryId(16); ?> <?php //now $blocks contain a list block for all categories. foreach ($blocks as $categoryId => $block) { ?> <div> <?php echo $block->toHtml() ?> </div> <?php } ?>
    – J. Doe
    Sep 29 '17 at 8:49
  • Check your template location is correct. Also put some strings in the template and check to see the output. Also make sure you typed the block directive instead of copy pasting it into cms page. Sep 29 '17 at 8:53
  • As is said the block gets executed. I tryed to put some string in html and output it with phps echo function. Both worked. The only thing that wont show is the category html code.
    – J. Doe
    Sep 29 '17 at 9:32
  • So this code wont work: `$layout = $this->getLayout(); echo $layout->createBlock('catalog/product_list')->setCategoryId(16)->toHtml(); ´
    – J. Doe
    Sep 29 '17 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.