Magento - Get Results View HTML for Product Collection

I get a list of magento identifiers from a web service. I load them into the $product_ids array, so I have something like this:

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

Then I can do it in the collection:

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

Using my Magento inspector, I saw that category pages use the Mage_Catalog_Block_Product_List class to display product lists. I would like to do something similar in my class. I tried downloading:

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

And then I tried to load the HTML result result as follows:

 $CollectionHTML = $ProductList->_toHtml(); 

But $CollectionHTML empty.

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

+4
source share
1 answer

Creating code in the correct order is much easier with Magento than trying to work with ugly legacy code. I would love to help you make the code right when you have specific questions. In addition, in the long run, technical debt will be much more expensive.

In any case, back to your problem.

In the Magento block, instances are not created, as in any application $myvar = new className ... almost never. This tutorial will help you better understand the layout and blocks of Magento.

But if you want to create a block, the way to do this is:

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

Now, after contacting your product collection, you should check how Mage_Catalog_Block_Product_List::_getProductCollection , because it uses multi-level navigation, and not just a collection of products.

Also, assuming that at least you are using a Magento controller and you are in a function, the following code will display the first page of products for the 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(); 

Display specific identifiers:

  • you are using the root category identifier for the $ category_id variable (also make sure the display root category is set (or another category identifier containing your product identifiers)
  • you can connect to the catalog_block_product_list_collection event to add your identification filter to the collection (this is called in the _beforeToHtml function)

But this whole construction is not continuous, and there are still some points that need attention (other child blocks, filters, etc.)

+3
source

Source: https://habr.com/ru/post/1440863/


All Articles