2

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?

2
+50

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;
    }
}
  • yeah this is the problem i have and i already know about it. I need a solution ;) – sissy Jun 5 '18 at 9:04
  • To code i posted will make your product listing to have the same collection as layeredNavigation has. – embed0 Jun 8 '18 at 7:25
0

The easiest way I can think of is to simply create (either manually of programmatically) categories for your brands and to use a cron task to assign / deassign your products to those categories based on the value of their brand attribute. This way you can let Magento do the frontend work for you.

  • I don't think it would be a feasible solution. The catalog product is pretty big (45000 products more or less) and it is heavily modified on a daily basis and moreover, the people who manages the Magento backend are not so expert, so it would be a mess to give them more things to manage. But thank you for your help. – sissy Jun 7 '18 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.