kaspi
kaspi

Reputation: 101

Magento2 - Custom Product Collection with Layered Navigation

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) ?

Upvotes: 10

Views: 3685

Answers (1)

TomS
TomS

Reputation: 681

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.

Upvotes: 1

Related Questions