Skip to content

Apply a custom collection to the layered navigation filter

By extending MagentoCatalogBlockProductListProduct; and replacing in the layout by my custom block I have been able to apply and load a custom collection in any category.

The issue is that the layered navigation doesn’t adapt to the new collection.

How to make that happen ?

I’ve tried to extend MagentoLayeredNavigationBlockNavigation

And in there i’m doing something like

    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoCatalogModelLayerResolver $layerResolver,
        MagentoCatalogModelLayerFilterList $filterList,
        MagentoCatalogModelLayerAvailabilityFlagInterface $visibilityFlag,
        MagentoFrameworkObjectManagerInterface $objectManager,
        MagentoFrameworkModuleManager $moduleManager,
        MagentoCatalogHelperData $helperData,
        Collection $customCollectionBlock,
        array $data
    )
    {
        parent::__construct($context, $layerResolver, $filterList, $visibilityFlag, $objectManager, $moduleManager, $data);
        $this->pageLayout         = $context->getPageConfig()->getPageLayout() ?: $this->getLayout()->getUpdate()->getPageLayout();
        $this->objectManager      = $objectManager;
        $this->moduleManager      = $moduleManager;
        $this->helperData = $helperData;

        $this->customCollectionBlock = $customCollectionBlock;
        $customCollectionFiltered = $customCollection->getFilteredProductCollection();

        $this->_catalogLayer->prepareProductCollection($customCollectionFiltered);
    }

But even with that the layered navigation still consider i have all the products from the original category in the collection.

What do I miss ? it’s been a while i’m struggling with that, and nobody seems to have a working sample for this solution.