Magento – Filter on custom product collection

filtergridlayered-navigationmagento2product-collection

I'm creating a custom brand page in which are listed product with a specific brand.

I've created a static page with a 2 column layout, with a left sidebar in which I'm putting filters and the main column contains the products grid.

This is the code for the content widget

{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" 
show_pager="0" 
products_count="100"
tagline="bla bla bla."  
logo="pub/media/brands/brand.png" 
template="MyVendor_MyTheme::product/widget/content/grid.phtml" 
conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`brand`,`operator`:`==`,`value`:`3847`^]^]"}}

for which I've overridden the Magento template grid.phtml

In the left sidebar I've put layered navigation

<referenceContainer name="sidebar.main">
             <block class="Magento\LayeredNavigation\Block\Navigation\Category" name="catalog.leftnav" before="-" template="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="layer/filter.phtml"/>
            </block>

        </referenceContainer>

for which I'm overriding the view.phtml template.
I'm able to start with a filtered collection in the view.phtml like this

$block->getLayer()->getProductCollection()->addFieldToFilter('brand','3847')

and filters returns the correct item count inside the block.

I've added a:

$block->getProductCollection()->addFieldToFilter('brand','3847')

in the grid.phtml just for debugging and the collection is correct in the grid.phtml template

The problem is that the filter is not applied to the grid.phtml block.

How do I "connect" the two blocks?

Best Answer

The source of your problem is that LayeredNavigation module uses it's own product collection.

namespace Vendor\Module\Controller\Index;    

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    /** @var \Magento\Framework\App\Action\Context
    protected $context;

    /** @var \Magento\Framework\View\Result\PageFactory */
    protected $pageFactory;

    /** @var \Magento\Catalog\Model\Layer\Resolver */
    protected $layerResolver;

    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        Resolver $layerResolver
    ) {
        $this->layerResolver = $layerResolver;
        $this->pageFactory = $pageFactory;
        $this->context = $context;
        parent::__construct($context);
    }

    public function execute()
    {
        $result = $this->pageFactory->create();
        $this->layerResolver->create('search');
        $collection = $this->layerResolver->get()->getProductCollection()->addFieldToFilter('brand','3847');
        $list = $result->getLayout()->getBlock('category.products.list');
        $list->setProductCollection($collection);
        return $result;
    }
}
Related Topic