0

I want custom product collection with layer navigation in custom page. I have checked many links already and followed with below link,

Layered navigation for custom collection on custom page - magento2

But every time I stuck on one place at Magento\Catalog\Block\Product\ListProduct

private function initializeProductCollection()
    {
        $layer = $this->getLayer();
        /* @var $layer Layer */
        if ($this->getShowRootCategory()) {
            $this->setCategoryId($this->_storeManager->getStore()->getRootCategoryId());
        }

        // if this is a product view page
        if ($this->_coreRegistry->registry('product')) {
            // get collection of categories this product is associated with
            $categories = $this->_coreRegistry->registry('product')
                ->getCategoryCollection()->setPage(1, 1)
                ->load();
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category
                $this->setCategoryId(current($categories->getIterator())->getId());
            }
        }

        $origCategory = null;
        if ($this->getCategoryId()) {
            try {
                $category = $this->categoryRepository->get($this->getCategoryId());
            } catch (NoSuchEntityException $e) {
                $category = null;
            }

            if ($category) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        $collection = $layer->getProductCollection();
        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
        $layer->setCurrentCategory($this->getCurrentCategory());
        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }

        $this->addToolbarBlock($collection);

        $this->_eventManager->dispatch(
            'catalog_block_product_list_collection',
            ['collection' => $collection]
        );

        return $collection;
    }

where every time I get $collection = $layer->getProductCollection() blank.

Also we have added sidebar main block in layout xml.

  <referenceContainer name="sidebar.main">

         <block class="MyVendor\MyModule\Block\Navigation\Custnavigation" name="custom.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>
     </referenceContainer> 
 <referenceContainer name="content">
            <block class="Magento\Catalog\Block\Product\ListProduct" name="custom.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
                <container name="category.product.list.additional" as="additional" />
                <block class="Magento\Framework\View\Element\RendererList" name="category.product.type.details.renderers" as="details.renderers">
                    <block class="Magento\Framework\View\Element\Template" as="default"/>
                </block>
                <block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
                    <block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
                </block>
                <action method="setToolbarBlockName">
                    <argument name="name" xsi:type="string">product_list_toolbar</argument>
                </action> 
            </block>
   </referenceContainer>

Can any one have any idea?

6
  • Did you got solution? Commented Dec 18, 2018 at 10:08
  • Yes, I got the solution. Commented Dec 18, 2018 at 10:11
  • Could you please explain in answer? Commented Dec 18, 2018 at 10:11
  • I am using _getProductCollection its working on list of product but not in Layered navigation count Commented Dec 18, 2018 at 10:12
  • Above code was working well for me. Just issue was with the url which we are using for custom collection in custom page. For example, I was using the url like 127.0.0.1/mage2/offer/index/id/1 So issue was with the id parameter, and which is taking in the core which was my exact issue. So After debugging this I have changed my URL to this, 127.0.0.1/mage2/offer/index/offer_id/1. So my issue fixed with this. Commented Dec 18, 2018 at 10:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.