2

I'm trying to render a default block from a phtml file, without luck. After a lot of research, I learnt that I can use this code inside a page to render the block I want:

{{block type=”catalog/product_list” category_id=”4″ template=”catalog/product/list.phtml”}}

How can I render the very same block, but from a PHTML file?

I've tried:

 $productOfCategoryBlock = $this->getLayout()->createBlock('Magento\Cms\Block\Block');
 $productOfCategoryBlock->setBlockId("catalog/product_list");
 $productOfCategoryBlock->setTemplate('catalog/product/list.phtml');
 echo $productOfCategoryBlock->toHtml();

With no luck.

What should I put inside setBlockId and setTemplate in order to replicate that? How can I pass the category ID I want to show?

Thanks for your help!

EDIT

My goal here is to render this block "catalog/product_list" by passing a category ID from the PHTML file. Something like this:

 $productOfCategoryBlock = $this->getLayout()->createBlock('Magento\Cms\Block\Block');
 $productOfCategoryBlock->setBlockId("catalog/product_list");
 $productOfCategoryBlock->setTemplate('catalog/product/list.phtml');
 $productOfCategoryBlock->setCategoryId(4);
 echo $productOfCategoryBlock->toHtml();
  • What this does? {{block type=”catalog/product_list” category_id=”4″ template=”catalog/product/list.phtml”}} guess get product list from category id 4 – Prathap Gunasekaran Apr 12 at 8:57
  • Yes, It's a block of products of one category (id 4) – Ruddle Apr 12 at 8:58
  • then you can call the staticblock in phml – Prathap Gunasekaran Apr 12 at 8:59
  • And that's exactly my question, how can I call the block from from the phtml? The code I provided does not work for me, and I don't know what I'm missing. – Ruddle Apr 12 at 9:03
1

Try this,

Call a static block in your phtml like below

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>

then in your static block

{{block type=”catalog/product_list” category_id=”4″ template=”catalog/product/list.phtml”}}

Hope it helps.

  • This works for rendering a fixed category, but if I have to create a static block how can I pass the "category ID" I want to show from the PHTML? – Ruddle Apr 12 at 9:17
  • If this works accept it as answer, for dynamic category you gotta to get data of product list by yourself only by passing category ids. – Prathap Gunasekaran Apr 12 at 9:22
  • Thanks Prathap, it works but unfortunately in this way it's missing the category ID I wanted to set in the PHTML. But you are right, I was not totally clear in my first question so I'll mark this as answer. I'll edit that now. I will be grateful if you care to elaborate more on "how to pass category id to block in PTHML". I already have the category ID in my phtml file. – Ruddle Apr 12 at 9:37
  • Has this answer useful or you need anything else? – Prathap Gunasekaran Apr 25 at 5:13

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.