Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento2 - Custom Product Collection with Layered Navigation

Tags:

magento2

I am trying to display Layered Navigation Block on custom page with custom product list.

For now i have custom controller, xml layout and template for product list.

  1. What do i need to insert into xml layout to display layered navigation block ?

<referenceContainer name="sidebar.main"> <!-- ??? --> </referenceContainer>

  1. How can i set custom product collection (filtered by attribute e.g. 'book' == 1) and use it (with layered navigation) on custom page (e.g. book list with author filter) ?
like image 329
kaspi Avatar asked Jul 11 '16 08:07

kaspi


People also ask

How do I create a layered navigation on the custom page in Magento 2?

Magento 2 general setting provides you with the option to configure layered navigation based on price navigation. In order to configure it, you need to go to Stores > Configuration > Catalog > Catalog > Layered Navigation.

How do I use custom conditions for product collection in Magento 2?

For Apply custom condition before product collection loaded, you need to create a di. xml file for plugin definition and plugin PHP file for add your custom logic. Under the di. xml file, You need to add plugin definition to create plugin beforeLoad( ) method.

What is collection magento2?

Collections in Magento are extremely useful, they allow you to retrieve objects such as products, categories, customers, etc. Retrieving these objects in a collection is a simple case of instantiating an instance of the object entity's factory class and using the model's 'getCollection' method, as follows.


1 Answers

The XML you need can be found in Magento/LayeredNavigation/view/frontend/layout/catalog_category_view_type_layered.xml

The snippet you'll need to add to your referenceContainer is:

<block class="Magento\LayeredNavigation\Block\Navigation\Category" name="catalog.leftnav" before="-" template="Magento_LayeredNavigation::layer/view.phtml">
            <block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalog.navigation.state" as="state" />
            <block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalog.navigation.renderer" as="renderer" template="Magento_LayeredNavigation::layer/filter.phtml"/>
</block>

I would suggest using a plugin to modify the render method of "Magento\LayeredNavigation\Block\Navigation\FilterRenderer" if you want to set a default filter.

like image 181
TomS Avatar answered Sep 23 '22 13:09

TomS