I already have a products collection and an empty page, how would I load the products list layout and use my own products collection?
EDIT:
Better explanation hopefully. I have a form to select year, make, model, and then category, which works well. I got a product collection, I just want to display the collection in a standard product listing using the currently loaded theme but the solution eludes me.
The Year, make model and associated products are stored in their own models. I didn't see a good way to store them in standard magento attributes as most products have several combinations of year, make, models.
EDIT2:
Got somewhere by adding this to my layout but by default it just shows all products. Possible to change the products displayed in the block?
<block class="Magento\Catalog\Block\Product\ListProduct" name="ymmResult" template="Magento_Catalog::product/list.phtml" />
Best Answer
Here is an example module that will allow you specify a product collection for a customized product list element, which you should be able to adapt to your needs. It works by extending the
ListProduct
class to allow it's backing collection to be set dynamically.File Structure:
registration.php
CustomList.php
is where we extendListProduct
by overriding theGetLoadedProductCollection
method and adding a method to set the product collection.Index.php
is an example of a controller where we set a product collection on theCustomList
block which will be placed in the layout file.module.xml
routes.xml
defines an example route where we want to display the product list.customprodlist_index_index.xml
is the layout for the above route. This is where we place our block element as well as some child blocks which I copied fromcatalog_category_view.xml
.