I am new in Magento 2. I need to render product list, and I'd like to use existing templates etc.
I found this: get the html of a product as it would render in category page
But it is for Magento 1.9, and not very specific.
I am new in Magento 2. I need to render product list, and I'd like to use existing templates etc.
I found this: get the html of a product as it would render in category page
But it is for Magento 1.9, and not very specific.
Take a look at the Magento\Catalog\Model\ProductRepository. You will use the getList() function to get the list of product models needed. The tricky part is setting up the Search Criteria. You will pass in the list of product IDs that you already have as a part of the criteria.
I figured it out:
Copy the child block search_result_list
from the file vendor/magento/module-catalog-search/view/frontend/layout/catalogsearch_result_index.xml
into the root block tag of your own layout file.
In the block file, add a getProductCollection
method:
public function getProductCollection(){
// Replace with your own logic.
$productIds = ['123', 'abc'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\Magento\Catalog\Model\Product');
$collection = $model->getCollection()
->addAttributeToFilter('sku', array('in' => $productIds))
->addAttributeToSelect('*')
->load();
return $collection;
}
In the template:
<?php
// Show products
$this->getChildBlock('search_result_list')->setCollection($this->getProductCollection());
echo $this->getChildHtml('search_result_list');
?>