1

Need suggestion on this!

I have overridden Magentos Catalog's Block Product ListProduct's _getProductCollection()

So when i search for a keyword in home page, my custom blocks _getProductCollection() should be called.

Custom Product Collection not Getting Filtered by Layered Navigation

Vendor\Catalog\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\ListProduct" type="Vendor\Catalog\Block\Product\ListProduct" />
<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Catalog\Block\Product\ProductList\Toolbar" />
<preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" type="Vendor\Catalog\Model\ResourceModel\Product\Collection" />
</config>

\Vendor\Catalog\Block\Product\ListProduct.php

namespace Vendor\Catalog\Block\Product;

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
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,

    \Vendor\LayeredNavigation\Helper\Data $tglssearchHelper,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $catalogResourceModelProductCollectionFactory,
    \Magento\Catalog\Model\Config $catalogConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
) {
    $this->tglssearchHelper = $tglssearchHelper;
    $this->catalogResourceModelProductCollectionFactory = $catalogResourceModelProductCollectionFactory;
    $this->catalogConfig = $catalogConfig;
    $this->storeManager = $storeManager;

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

 public function _getProductCollection()
{
    echo ' Tagcataloggetproc colle';
    //problem here...this fun not at all getting called!
}

\Vendor\Catalog\Block\Product\ProductList\Toolbar.php

namespace Vendor\Catalog\Block\Product\ProductList;

use Magento\Catalog\Helper\Product\ProductList;
use Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
    public function __construct(
    /* passing all Constructors parameters to the parent class -Magento\Catalog\Block\Product\ProductList\Toolbar.php*/
    \Magento\Framework\View\Element\Template\Context $context,
     \Magento\Catalog\Model\Session $catalogSession,
    \Magento\Catalog\Model\Config $catalogConfig,
    ToolbarModel $toolbarModel,
    \Magento\Framework\Url\EncoderInterface $urlEncoder,
    ProductList $productListHelper,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    //\Magento\Framework\Registry $registry,
    //\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Vendor\LayeredNavigation\Helper\Data $tglssearchHelper, 
    array $data = []
)  {
        $this->tglssearchHelper = $tglssearchHelper;
        $this->_catalogSession = $catalogSession;
        $this->_catalogConfig = $catalogConfig;
        $this->_toolbarModel = $toolbarModel;
        $this->_urlEncoder = $urlEncoder;
        $this->_productListHelper = $productListHelper;
        $this->_postDataHelper = $postDataHelper;
        //$this->_registry = $registry;
        //$this->scopeConfig = $scopeConfig;
        parent::__construct($context,$catalogSession,$catalogConfig,$toolbarModel,$urlEncoder,$productListHelper,$postDataHelper,$data);
   }

   //Overriden methods below:
  getAvailableOrders()

 getOrderDirection($sort_id)

setDefaultOrder($field) 

isOrderCurrent($key) 

}

\Vendor\Catalog\Model\ResourceModel\Product\Collection.php

<?php
 namespace Vendor\Catalog\Model\ResourceModel\Product;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{

   protected $tglssearchHelper;

public function __construct(
    \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    \Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\Eav\Model\Config $eavConfig,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Eav\Model\EntityFactory $eavEntityFactory,
    \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
    \Magento\Framework\Validator\UniversalFactory $universalFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Module\Manager $moduleManager,
    \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
    \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\Stdlib\DateTime $dateTime,
    \Magento\Customer\Api\GroupManagementInterface $groupManagement,
    \Vendor\LayeredNavigation\Helper\Data $tglssearchHelper,
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
) {
    $this->tglssearchHelper = $tglssearchHelper;
    parent::__construct(
        $entityFactory,
        $logger,
        $fetchStrategy,
        $eventManager,
        $eavConfig,
        $resource,
        $eavEntityFactory,
        $resourceHelper,
        $universalFactory,
        $storeManager,
        $moduleManager,
        $catalogProductFlatState,
        $scopeConfig,
        $productOptionFactory,
        $catalogUrl,
        $localeDate,
        $customerSession,
        $dateTime,
        $groupManagement,
        $connection
    );
}

    //    my custom methods()...

Now Overriding LayeredNavigation

\Vendor\LayeredNavigation\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\LayeredNavigation\Block\Navigation" type="Vendor\LayeredNavigation\Block\Navigation" />
</config>

in Vendor\LayeredNavigation\Block\Navigation.php

Have _prepareLayout() as is from core and overridden canShowBlock()

Now,

1) Why is the custom product collections not coming from ListProduct()?

2) collection not getting filtered by Layered Navigation?

Please suggest where is the issue in collection!

  • Have you find the solution for it? If so, please share your answer – Nalin Savaliya Nov 7 '16 at 7:35
  • No..only in my Catalog product its gets filtered, but when i search for a product, my custom product still not loads... – Sushivam Nov 7 '16 at 9:11
  • Got the solution? @NalinSavaliya – Yasar Arafath Dec 18 '18 at 10:09
  • @YasarArafath I did it 2 year back, you may get solution from it. magento.stackexchange.com/questions/142562/… – Nalin Savaliya Dec 18 '18 at 12:28

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.