2
 <?php

namespace Vendor\QuickAccess\Block;

use Magento\Catalog\Api\CategoryRepositoryInterface;
class Products extends \Magento\Catalog\Block\Product\ListProduct{

    protected $_collection;

    protected $categoryRepository;

    protected $_resource;

    public function __construct(
            \Magento\Catalog\Block\Product\Context $context, 
            \Magento\Framework\Data\Helper\PostHelper $postDataHelper, 
            \Magento\Catalog\Model\Layer\Resolver $layerResolver, 
            CategoryRepositoryInterface $categoryRepository,
            \Magento\Framework\Url\Helper\Data $urlHelper, 
            \Magento\Catalog\Model\ResourceModel\Product\Collection $collection, 
            \Magento\Framework\App\ResourceConnection $resource,
            array $data = []
    ) {
        $this->categoryRepository = $categoryRepository;
        $this->_collection = $collection;
        $this->_resource = $resource;
        $this->_coreRegistry = $context->getRegistry();

        parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Top Products'));

        if ($this->getTopProductCollection()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'topproducts.list.pager'
            )->setAvailableLimit(array(2=>2,9=>9,15=>15,24=>24,30=>30))
                ->setShowPerPage(true)->setCollection(
                $this->getTopProductCollection()
            );
            $this->setChild('pager', $pager);
            $this->getTopProductCollection()->load();
        }
        return $this;
    }

    /*
    *  get pager html for toolbar
    */

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    public function getProducts() {

        $category_id = $this->getData("category_id");
        $collection = clone $this->_collection;

        if(!$category_id) {
            $category_id = $this->_storeManager->getStore()->getRootCategoryId();
        }
        $category = $this->categoryRepository->get($category_id);
        if(isset($category) && $category) {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addUrlRewrite()
                ->addCategoryFilter($category);
        } else {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addUrlRewrite();
        }

        return $collection;
    }

    /*
     * Load and return product collection 
     */
    public function getLoadedProductCollection() {  
        return $this->getProducts();
    }

    /**
     * Get image helper
     */
    public function getImageHelper() {
        return $this->_imageHelper;
    }

     /*
     * Load and return product collection 
     */
    public function getTopProductCollection() {  
        $collection = $this->getProducts();
        $collection->addAttributeToFilter('is_top_eliquid', 1);

        return $collection;
    }  



}

I am trying to set layered navigation for custom product collection like catalogsearch through xml frontname_controller_index.xml added this code but it is showing error, how can i add the filters for custom collection please suggest me thanks

<referenceContainer name="sidebar.main">
            <block class="Magento\LayeredNavigation\Block\Navigation\Search" name="catalogsearch.leftnav" before="-" template="layer/view.phtml">
            <block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalogsearch.navigation.state" as="state" />
            <block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalogsearch.navigation.renderer" as="renderer" template="layer/filter.phtml"/>
            </block>
        </referenceContainer>

Your Answer

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

Browse other questions tagged or ask your own question.