app/code/Vendor/ModuleName/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\ModuleName\Block\Product\ListProducts" />
<preference for="Magento\Theme\Block\Html\Pager" type="Vendor\Modulename\Block\Product\Pager" />
<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Modulename\Block\Product\Toolbar" />
</config>
Vendor\ModuleName\Block\Product\ListProducts.php
<?php
namespace Vendor\ModuleName\Block\Product;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Registry;
use Magento\Framework\Url\Helper\Data;
class ListProducts extends ListProduct
{
public $registry;
public $cache;
/** @var \Magento\Catalog\Api\ProductRepositoryInterface */
public $productRepository;
/** @var \Magento\Framework\Api\SearchCriteriaBuilder */
public $searchCriteriaBuilder;
/** @var \Magento\Framework\Api\Search\FilterGroupBuilder */
public $filterGroupBuilder;
/** @var \Magento\Framework\Api\FilterBuilder */
public $filterBuilder;
public $collectionFactory;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
CacheInterface $cache,
Registry $registry,
Context $context,
PostHelper $postDataHelper,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
Data $urlHelper,
array $data = []
) {
$this->collectionFactory = $collectionFactory;
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->cache = $cache;
$this->registry = $registry;
parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
}
public function getLoadedProductCollection()
{
$collection = parent::getLoadedProductCollection();
// Add Your Filters
return $collection;
}
}
Vendor\ModuleName\Block\Product\Pager.php
<?php
namespace Vendor\ModuleName\Block\Product;
class Pager extends \Magento\Theme\Block\Html\Pager
{
public function getCollection()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->create('\Magento\Framework\App\RequestInterface');
$field = $request->getParam('field', null);
$attributeValue = $request->getParam($field);
$collection = $objectManager->create('Magento\Catalog\Model\Product')->getCollection();
// Add the Same Filters here To Override the Count of Products
return $collection;
}
}
Vendor\ModuleName\Block\Product\Toolbar.php
<?php
namespace Vendor\ModuleName\Block\Product;
class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
public function getCollection()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->create('\Magento\Framework\App\RequestInterface');
$field = $request->getParam('field', null);
$attributeValue = $request->getParam($field);
$collection = $objectManager->create('Magento\Catalog\Model\Product')
->getCollection()
->addAttributeToSelect('*');
// Add Same Filter to Override the Paginations
return $collection;
}
}
In This Way The Products Collection, Product Count, Pagination and the Layered Navigation will be Accurate.
catalog_block_product_list_collection
to filter you products