0

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.

2 Answers 2

0

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.

1
  • Thanks for the hint about the Search Criteria, but what I am missing, is the rendering part, not the search.
    – geon
    Commented Mar 28, 2017 at 6:46
0

I figured it out:

Copy the child block search_result_listfrom 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');

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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