Magento 2 - Render custom product collection via list.phtml





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







11















The product grid of a category page (frontend) is rendered via layout in catalog_category_view.xml.



Lets say I have a custom product collection (which I got via




ProductRepositoryInterface::getList($searchCriteria) method




in a custom block class and want to render this collection. The rendered result should be displayed as a product grid on frontend (just like any category page).



How can this be done ?



By looking into catalog_category_view.xml there are two significant lines, which are responsible for rendering a product collection:



<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


How can I provide my custom product collection to these template files, so they render my collection ?



Correct me, if I am wrong on this.



This is how my block code looks like:



<?php
namespace ModMod1Block;
use MagentoFrameworkViewElementTemplate;
class Main extends Template
{
protected $_filterBuilder;
protected $_filterGroupArray;
protected $_filterGroupBuilder;
protected $_searchCriteriaBuilder;
protected $_productRepository;
protected $_productFactory;
protected $_list;

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
MagentoFrameworkApiSearchFilterGroupBuilder $filterGroupBuilder,
MagentoFrameworkApiFilterBuilder $filterBuilder,
MagentoCatalogModelProductFactory $productFactory,
array $data = )
{
$this->_productRepository = $productRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_filterGroupBuilder = $filterGroupBuilder;
$this->_filterBuilder = $filterBuilder;
parent::__construct($context, $data);
}

public function getLoadedProductCollection(){
$searchCrit = $this->buildSearchCriteria('','','','','','5-',1);
$list = $this->_productRepository->getList($searchCrit);
return $list;
}
public function buildSearchCriteria(...){
....
return $searchCriteria;
}
}









share|improve this question
















bumped to the homepage by Community 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Are you asking how to replace products on a category page or how to essentially have another version of the category controller?

    – Josh Davenport
    Sep 19 '17 at 15:40











  • Pagination and layer navigation never come right if you edit collection in list.phtml or corresponding block. So make sure the answer you're accepting does both of the things above.Regards.

    – Vivek Kumar
    Sep 22 '17 at 7:22


















11















The product grid of a category page (frontend) is rendered via layout in catalog_category_view.xml.



Lets say I have a custom product collection (which I got via




ProductRepositoryInterface::getList($searchCriteria) method




in a custom block class and want to render this collection. The rendered result should be displayed as a product grid on frontend (just like any category page).



How can this be done ?



By looking into catalog_category_view.xml there are two significant lines, which are responsible for rendering a product collection:



<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


How can I provide my custom product collection to these template files, so they render my collection ?



Correct me, if I am wrong on this.



This is how my block code looks like:



<?php
namespace ModMod1Block;
use MagentoFrameworkViewElementTemplate;
class Main extends Template
{
protected $_filterBuilder;
protected $_filterGroupArray;
protected $_filterGroupBuilder;
protected $_searchCriteriaBuilder;
protected $_productRepository;
protected $_productFactory;
protected $_list;

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
MagentoFrameworkApiSearchFilterGroupBuilder $filterGroupBuilder,
MagentoFrameworkApiFilterBuilder $filterBuilder,
MagentoCatalogModelProductFactory $productFactory,
array $data = )
{
$this->_productRepository = $productRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_filterGroupBuilder = $filterGroupBuilder;
$this->_filterBuilder = $filterBuilder;
parent::__construct($context, $data);
}

public function getLoadedProductCollection(){
$searchCrit = $this->buildSearchCriteria('','','','','','5-',1);
$list = $this->_productRepository->getList($searchCrit);
return $list;
}
public function buildSearchCriteria(...){
....
return $searchCriteria;
}
}









share|improve this question
















bumped to the homepage by Community 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Are you asking how to replace products on a category page or how to essentially have another version of the category controller?

    – Josh Davenport
    Sep 19 '17 at 15:40











  • Pagination and layer navigation never come right if you edit collection in list.phtml or corresponding block. So make sure the answer you're accepting does both of the things above.Regards.

    – Vivek Kumar
    Sep 22 '17 at 7:22














11












11








11


1






The product grid of a category page (frontend) is rendered via layout in catalog_category_view.xml.



Lets say I have a custom product collection (which I got via




ProductRepositoryInterface::getList($searchCriteria) method




in a custom block class and want to render this collection. The rendered result should be displayed as a product grid on frontend (just like any category page).



How can this be done ?



By looking into catalog_category_view.xml there are two significant lines, which are responsible for rendering a product collection:



<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


How can I provide my custom product collection to these template files, so they render my collection ?



Correct me, if I am wrong on this.



This is how my block code looks like:



<?php
namespace ModMod1Block;
use MagentoFrameworkViewElementTemplate;
class Main extends Template
{
protected $_filterBuilder;
protected $_filterGroupArray;
protected $_filterGroupBuilder;
protected $_searchCriteriaBuilder;
protected $_productRepository;
protected $_productFactory;
protected $_list;

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
MagentoFrameworkApiSearchFilterGroupBuilder $filterGroupBuilder,
MagentoFrameworkApiFilterBuilder $filterBuilder,
MagentoCatalogModelProductFactory $productFactory,
array $data = )
{
$this->_productRepository = $productRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_filterGroupBuilder = $filterGroupBuilder;
$this->_filterBuilder = $filterBuilder;
parent::__construct($context, $data);
}

public function getLoadedProductCollection(){
$searchCrit = $this->buildSearchCriteria('','','','','','5-',1);
$list = $this->_productRepository->getList($searchCrit);
return $list;
}
public function buildSearchCriteria(...){
....
return $searchCriteria;
}
}









share|improve this question
















The product grid of a category page (frontend) is rendered via layout in catalog_category_view.xml.



Lets say I have a custom product collection (which I got via




ProductRepositoryInterface::getList($searchCriteria) method




in a custom block class and want to render this collection. The rendered result should be displayed as a product grid on frontend (just like any category page).



How can this be done ?



By looking into catalog_category_view.xml there are two significant lines, which are responsible for rendering a product collection:



<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


How can I provide my custom product collection to these template files, so they render my collection ?



Correct me, if I am wrong on this.



This is how my block code looks like:



<?php
namespace ModMod1Block;
use MagentoFrameworkViewElementTemplate;
class Main extends Template
{
protected $_filterBuilder;
protected $_filterGroupArray;
protected $_filterGroupBuilder;
protected $_searchCriteriaBuilder;
protected $_productRepository;
protected $_productFactory;
protected $_list;

public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogApiProductRepositoryInterface $productRepository,
MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
MagentoFrameworkApiSearchFilterGroupBuilder $filterGroupBuilder,
MagentoFrameworkApiFilterBuilder $filterBuilder,
MagentoCatalogModelProductFactory $productFactory,
array $data = )
{
$this->_productRepository = $productRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_filterGroupBuilder = $filterGroupBuilder;
$this->_filterBuilder = $filterBuilder;
parent::__construct($context, $data);
}

public function getLoadedProductCollection(){
$searchCrit = $this->buildSearchCriteria('','','','','','5-',1);
$list = $this->_productRepository->getList($searchCrit);
return $list;
}
public function buildSearchCriteria(...){
....
return $searchCriteria;
}
}






magento-2.1 custom product-collection render






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 '18 at 15:16









Abhishek Panchal

3,6303929




3,6303929










asked Sep 13 '17 at 18:13









shahir hajirshahir hajir

4761934




4761934





bumped to the homepage by Community 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 6 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Are you asking how to replace products on a category page or how to essentially have another version of the category controller?

    – Josh Davenport
    Sep 19 '17 at 15:40











  • Pagination and layer navigation never come right if you edit collection in list.phtml or corresponding block. So make sure the answer you're accepting does both of the things above.Regards.

    – Vivek Kumar
    Sep 22 '17 at 7:22



















  • Are you asking how to replace products on a category page or how to essentially have another version of the category controller?

    – Josh Davenport
    Sep 19 '17 at 15:40











  • Pagination and layer navigation never come right if you edit collection in list.phtml or corresponding block. So make sure the answer you're accepting does both of the things above.Regards.

    – Vivek Kumar
    Sep 22 '17 at 7:22

















Are you asking how to replace products on a category page or how to essentially have another version of the category controller?

– Josh Davenport
Sep 19 '17 at 15:40





Are you asking how to replace products on a category page or how to essentially have another version of the category controller?

– Josh Davenport
Sep 19 '17 at 15:40













Pagination and layer navigation never come right if you edit collection in list.phtml or corresponding block. So make sure the answer you're accepting does both of the things above.Regards.

– Vivek Kumar
Sep 22 '17 at 7:22





Pagination and layer navigation never come right if you edit collection in list.phtml or corresponding block. So make sure the answer you're accepting does both of the things above.Regards.

– Vivek Kumar
Sep 22 '17 at 7:22










4 Answers
4






active

oldest

votes


















0














You can try this:



Update your catalog_category_view.xml



<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<block class="Your(Mod)Namespace(Mod1)BlockYourBlockFileName(Main)" name="your.category.products.list" template="Magento_Catalog::product/yourFile.phtml" />
</block>


And call yourFile.phtml in product/list.phtml:



<?php echo $this->getChildHtml('your.category.products.list'); ?>


Now, you can use your function in yourfile.phtml like this:



$block->yourfunction();


Example:



$block->getLoadedProductCollection();


Hope this can help you.






share|improve this answer































    0














    The best approach would be:



    Update your catalog_category_view.xml and replace MagentoCatalogBlockProductListProduct with ModMod1BlockMain



    before:



    <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


    after:



    block class="ModMod1BlockMain" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


    Your Main class MUST extend MagentoCatalogBlockProductListProduct



    Then you will need to rewrite the method getLayer() to your own.




    FYI: your class ModMod1BlockMain needs some refactoring.




    Create your own new Layer class that will extend MagentoCatalogModelLayer and play around with the collections.



    /**
    * Retrieve current layer product collection
    *
    * @return MagentoCatalogModelResourceModelProductCollection
    */
    public function getProductCollection()


    and



    /**
    * Initialize product collection
    *
    * @param MagentoCatalogModelResourceModelProductCollection $collection
    * @return MagentoCatalogModelLayer
    */
    public function prepareProductCollection($collection)





    share|improve this answer































      0














      Basically initially you want to create a controller or even a basic CMS page so you have a page to show your collection.



      From there you can use your custom block which creates your collection combined with the list.phtml template file to render the products on your page.



      {{block class="VendorModuleBlockProductCollection" template="Magento_Catalog::product/list.phtml" name="specialproducts"}} 


      Add above xml where VendorModuleBlockProductCollection is the custom block code where your collection is created to xml for that page.



      The list.phtml file gathers the collection from your block with below line of code:



      $_productCollection = $block->getLoadedProductCollection();


      So long as your block has the getLoadedProductCollection() function returning your collection of products the list.phtml file will then loop through each product in your collection rendering them in a default Magento product grid.



      For your block code try updating below line to include getItems():



      $list = $this->_productRepository->getList($searchCrit)->getItems();





      share|improve this answer


























      • I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

        – shahir hajir
        Sep 21 '17 at 14:09













      • see updated post

        – shahir hajir
        Sep 21 '17 at 14:59











      • Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

        – harri
        Sep 21 '17 at 15:40











      • @shahirhajir Did you try with getItems?

        – harri
        Sep 25 '17 at 13:20











      • Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

        – shahir hajir
        Sep 25 '17 at 20:46



















      -1














      You can render product collection in your phtml file like this




      $product_collection = $block->getLoadedProductCollection();







      share|improve this answer
























      • Not helpful at all

        – shahir hajir
        Sep 25 '17 at 20:32












      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f193127%2fmagento-2-render-custom-product-collection-via-list-phtml%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You can try this:



      Update your catalog_category_view.xml



      <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
      <block class="Your(Mod)Namespace(Mod1)BlockYourBlockFileName(Main)" name="your.category.products.list" template="Magento_Catalog::product/yourFile.phtml" />
      </block>


      And call yourFile.phtml in product/list.phtml:



      <?php echo $this->getChildHtml('your.category.products.list'); ?>


      Now, you can use your function in yourfile.phtml like this:



      $block->yourfunction();


      Example:



      $block->getLoadedProductCollection();


      Hope this can help you.






      share|improve this answer




























        0














        You can try this:



        Update your catalog_category_view.xml



        <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
        <block class="Your(Mod)Namespace(Mod1)BlockYourBlockFileName(Main)" name="your.category.products.list" template="Magento_Catalog::product/yourFile.phtml" />
        </block>


        And call yourFile.phtml in product/list.phtml:



        <?php echo $this->getChildHtml('your.category.products.list'); ?>


        Now, you can use your function in yourfile.phtml like this:



        $block->yourfunction();


        Example:



        $block->getLoadedProductCollection();


        Hope this can help you.






        share|improve this answer


























          0












          0








          0







          You can try this:



          Update your catalog_category_view.xml



          <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
          <block class="Your(Mod)Namespace(Mod1)BlockYourBlockFileName(Main)" name="your.category.products.list" template="Magento_Catalog::product/yourFile.phtml" />
          </block>


          And call yourFile.phtml in product/list.phtml:



          <?php echo $this->getChildHtml('your.category.products.list'); ?>


          Now, you can use your function in yourfile.phtml like this:



          $block->yourfunction();


          Example:



          $block->getLoadedProductCollection();


          Hope this can help you.






          share|improve this answer













          You can try this:



          Update your catalog_category_view.xml



          <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
          <block class="Your(Mod)Namespace(Mod1)BlockYourBlockFileName(Main)" name="your.category.products.list" template="Magento_Catalog::product/yourFile.phtml" />
          </block>


          And call yourFile.phtml in product/list.phtml:



          <?php echo $this->getChildHtml('your.category.products.list'); ?>


          Now, you can use your function in yourfile.phtml like this:



          $block->yourfunction();


          Example:



          $block->getLoadedProductCollection();


          Hope this can help you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 22 '17 at 2:10









          TriAnhHuynhTriAnhHuynh

          14810




          14810

























              0














              The best approach would be:



              Update your catalog_category_view.xml and replace MagentoCatalogBlockProductListProduct with ModMod1BlockMain



              before:



              <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


              after:



              block class="ModMod1BlockMain" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


              Your Main class MUST extend MagentoCatalogBlockProductListProduct



              Then you will need to rewrite the method getLayer() to your own.




              FYI: your class ModMod1BlockMain needs some refactoring.




              Create your own new Layer class that will extend MagentoCatalogModelLayer and play around with the collections.



              /**
              * Retrieve current layer product collection
              *
              * @return MagentoCatalogModelResourceModelProductCollection
              */
              public function getProductCollection()


              and



              /**
              * Initialize product collection
              *
              * @param MagentoCatalogModelResourceModelProductCollection $collection
              * @return MagentoCatalogModelLayer
              */
              public function prepareProductCollection($collection)





              share|improve this answer




























                0














                The best approach would be:



                Update your catalog_category_view.xml and replace MagentoCatalogBlockProductListProduct with ModMod1BlockMain



                before:



                <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


                after:



                block class="ModMod1BlockMain" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


                Your Main class MUST extend MagentoCatalogBlockProductListProduct



                Then you will need to rewrite the method getLayer() to your own.




                FYI: your class ModMod1BlockMain needs some refactoring.




                Create your own new Layer class that will extend MagentoCatalogModelLayer and play around with the collections.



                /**
                * Retrieve current layer product collection
                *
                * @return MagentoCatalogModelResourceModelProductCollection
                */
                public function getProductCollection()


                and



                /**
                * Initialize product collection
                *
                * @param MagentoCatalogModelResourceModelProductCollection $collection
                * @return MagentoCatalogModelLayer
                */
                public function prepareProductCollection($collection)





                share|improve this answer


























                  0












                  0








                  0







                  The best approach would be:



                  Update your catalog_category_view.xml and replace MagentoCatalogBlockProductListProduct with ModMod1BlockMain



                  before:



                  <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


                  after:



                  block class="ModMod1BlockMain" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


                  Your Main class MUST extend MagentoCatalogBlockProductListProduct



                  Then you will need to rewrite the method getLayer() to your own.




                  FYI: your class ModMod1BlockMain needs some refactoring.




                  Create your own new Layer class that will extend MagentoCatalogModelLayer and play around with the collections.



                  /**
                  * Retrieve current layer product collection
                  *
                  * @return MagentoCatalogModelResourceModelProductCollection
                  */
                  public function getProductCollection()


                  and



                  /**
                  * Initialize product collection
                  *
                  * @param MagentoCatalogModelResourceModelProductCollection $collection
                  * @return MagentoCatalogModelLayer
                  */
                  public function prepareProductCollection($collection)





                  share|improve this answer













                  The best approach would be:



                  Update your catalog_category_view.xml and replace MagentoCatalogBlockProductListProduct with ModMod1BlockMain



                  before:



                  <block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


                  after:



                  block class="ModMod1BlockMain" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">


                  Your Main class MUST extend MagentoCatalogBlockProductListProduct



                  Then you will need to rewrite the method getLayer() to your own.




                  FYI: your class ModMod1BlockMain needs some refactoring.




                  Create your own new Layer class that will extend MagentoCatalogModelLayer and play around with the collections.



                  /**
                  * Retrieve current layer product collection
                  *
                  * @return MagentoCatalogModelResourceModelProductCollection
                  */
                  public function getProductCollection()


                  and



                  /**
                  * Initialize product collection
                  *
                  * @param MagentoCatalogModelResourceModelProductCollection $collection
                  * @return MagentoCatalogModelLayer
                  */
                  public function prepareProductCollection($collection)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 11 '18 at 19:21









                  Thiago LimaThiago Lima

                  482210




                  482210























                      0














                      Basically initially you want to create a controller or even a basic CMS page so you have a page to show your collection.



                      From there you can use your custom block which creates your collection combined with the list.phtml template file to render the products on your page.



                      {{block class="VendorModuleBlockProductCollection" template="Magento_Catalog::product/list.phtml" name="specialproducts"}} 


                      Add above xml where VendorModuleBlockProductCollection is the custom block code where your collection is created to xml for that page.



                      The list.phtml file gathers the collection from your block with below line of code:



                      $_productCollection = $block->getLoadedProductCollection();


                      So long as your block has the getLoadedProductCollection() function returning your collection of products the list.phtml file will then loop through each product in your collection rendering them in a default Magento product grid.



                      For your block code try updating below line to include getItems():



                      $list = $this->_productRepository->getList($searchCrit)->getItems();





                      share|improve this answer


























                      • I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

                        – shahir hajir
                        Sep 21 '17 at 14:09













                      • see updated post

                        – shahir hajir
                        Sep 21 '17 at 14:59











                      • Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

                        – harri
                        Sep 21 '17 at 15:40











                      • @shahirhajir Did you try with getItems?

                        – harri
                        Sep 25 '17 at 13:20











                      • Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

                        – shahir hajir
                        Sep 25 '17 at 20:46
















                      0














                      Basically initially you want to create a controller or even a basic CMS page so you have a page to show your collection.



                      From there you can use your custom block which creates your collection combined with the list.phtml template file to render the products on your page.



                      {{block class="VendorModuleBlockProductCollection" template="Magento_Catalog::product/list.phtml" name="specialproducts"}} 


                      Add above xml where VendorModuleBlockProductCollection is the custom block code where your collection is created to xml for that page.



                      The list.phtml file gathers the collection from your block with below line of code:



                      $_productCollection = $block->getLoadedProductCollection();


                      So long as your block has the getLoadedProductCollection() function returning your collection of products the list.phtml file will then loop through each product in your collection rendering them in a default Magento product grid.



                      For your block code try updating below line to include getItems():



                      $list = $this->_productRepository->getList($searchCrit)->getItems();





                      share|improve this answer


























                      • I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

                        – shahir hajir
                        Sep 21 '17 at 14:09













                      • see updated post

                        – shahir hajir
                        Sep 21 '17 at 14:59











                      • Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

                        – harri
                        Sep 21 '17 at 15:40











                      • @shahirhajir Did you try with getItems?

                        – harri
                        Sep 25 '17 at 13:20











                      • Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

                        – shahir hajir
                        Sep 25 '17 at 20:46














                      0












                      0








                      0







                      Basically initially you want to create a controller or even a basic CMS page so you have a page to show your collection.



                      From there you can use your custom block which creates your collection combined with the list.phtml template file to render the products on your page.



                      {{block class="VendorModuleBlockProductCollection" template="Magento_Catalog::product/list.phtml" name="specialproducts"}} 


                      Add above xml where VendorModuleBlockProductCollection is the custom block code where your collection is created to xml for that page.



                      The list.phtml file gathers the collection from your block with below line of code:



                      $_productCollection = $block->getLoadedProductCollection();


                      So long as your block has the getLoadedProductCollection() function returning your collection of products the list.phtml file will then loop through each product in your collection rendering them in a default Magento product grid.



                      For your block code try updating below line to include getItems():



                      $list = $this->_productRepository->getList($searchCrit)->getItems();





                      share|improve this answer















                      Basically initially you want to create a controller or even a basic CMS page so you have a page to show your collection.



                      From there you can use your custom block which creates your collection combined with the list.phtml template file to render the products on your page.



                      {{block class="VendorModuleBlockProductCollection" template="Magento_Catalog::product/list.phtml" name="specialproducts"}} 


                      Add above xml where VendorModuleBlockProductCollection is the custom block code where your collection is created to xml for that page.



                      The list.phtml file gathers the collection from your block with below line of code:



                      $_productCollection = $block->getLoadedProductCollection();


                      So long as your block has the getLoadedProductCollection() function returning your collection of products the list.phtml file will then loop through each product in your collection rendering them in a default Magento product grid.



                      For your block code try updating below line to include getItems():



                      $list = $this->_productRepository->getList($searchCrit)->getItems();






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 8 '18 at 9:55









                      Teja Bhagavan Kollepara

                      2,99542050




                      2,99542050










                      answered Sep 20 '17 at 15:51









                      harriharri

                      3,12911660




                      3,12911660













                      • I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

                        – shahir hajir
                        Sep 21 '17 at 14:09













                      • see updated post

                        – shahir hajir
                        Sep 21 '17 at 14:59











                      • Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

                        – harri
                        Sep 21 '17 at 15:40











                      • @shahirhajir Did you try with getItems?

                        – harri
                        Sep 25 '17 at 13:20











                      • Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

                        – shahir hajir
                        Sep 25 '17 at 20:46



















                      • I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

                        – shahir hajir
                        Sep 21 '17 at 14:09













                      • see updated post

                        – shahir hajir
                        Sep 21 '17 at 14:59











                      • Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

                        – harri
                        Sep 21 '17 at 15:40











                      • @shahirhajir Did you try with getItems?

                        – harri
                        Sep 25 '17 at 13:20











                      • Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

                        – shahir hajir
                        Sep 25 '17 at 20:46

















                      I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

                      – shahir hajir
                      Sep 21 '17 at 14:09







                      I did as you said. My block has the ' getLoadedProductCollection()' function which returns 'ProductRepositoryInterface::getList()'. But I get an Error : Uncaught Error: Call to undefined method MagentoFrameworkApiSearchResults::count() in ... Looks like this is not the right approach.

                      – shahir hajir
                      Sep 21 '17 at 14:09















                      see updated post

                      – shahir hajir
                      Sep 21 '17 at 14:59





                      see updated post

                      – shahir hajir
                      Sep 21 '17 at 14:59













                      Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

                      – harri
                      Sep 21 '17 at 15:40





                      Maybe try changing $list = $this->_productRepository->getList($searchCrit)->getItems();

                      – harri
                      Sep 21 '17 at 15:40













                      @shahirhajir Did you try with getItems?

                      – harri
                      Sep 25 '17 at 13:20





                      @shahirhajir Did you try with getItems?

                      – harri
                      Sep 25 '17 at 13:20













                      Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

                      – shahir hajir
                      Sep 25 '17 at 20:46





                      Not working. $list = $this->_productRepository->getList($searchCrit)->getItems(); returns ProductInterface array and this array has no count() funtion. Uncaught Error: Call to a member function count() on array in ...list.phtml on line 22. On line 22 in list.phtml we have: $_productCollection->count()

                      – shahir hajir
                      Sep 25 '17 at 20:46











                      -1














                      You can render product collection in your phtml file like this




                      $product_collection = $block->getLoadedProductCollection();







                      share|improve this answer
























                      • Not helpful at all

                        – shahir hajir
                        Sep 25 '17 at 20:32
















                      -1














                      You can render product collection in your phtml file like this




                      $product_collection = $block->getLoadedProductCollection();







                      share|improve this answer
























                      • Not helpful at all

                        – shahir hajir
                        Sep 25 '17 at 20:32














                      -1












                      -1








                      -1







                      You can render product collection in your phtml file like this




                      $product_collection = $block->getLoadedProductCollection();







                      share|improve this answer













                      You can render product collection in your phtml file like this




                      $product_collection = $block->getLoadedProductCollection();








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 25 '17 at 11:15









                      Divya MuralidharanDivya Muralidharan

                      3991318




                      3991318













                      • Not helpful at all

                        – shahir hajir
                        Sep 25 '17 at 20:32



















                      • Not helpful at all

                        – shahir hajir
                        Sep 25 '17 at 20:32

















                      Not helpful at all

                      – shahir hajir
                      Sep 25 '17 at 20:32





                      Not helpful at all

                      – shahir hajir
                      Sep 25 '17 at 20:32


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f193127%2fmagento-2-render-custom-product-collection-via-list-phtml%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Alcázar de San Juan

                      Griza ansero

                      Heinkel He 51