8

I have one category in system... For example EMT. There are many products assigned to it. Now I want to filter some products for particular customers for that category. I mean only specific products should be display in that category for some customers. It should display all the products to guest users.

How can I do that so all the collection should filter with proper layered navigation and toolbar collection.

Can anyone suggest the way to do this with files I need to override?

enter image description here

4
  • you have pre list of product to show to customer ? Commented Nov 2, 2017 at 7:42
  • you can use event catalog_block_product_list_collection to filter you products Commented Nov 2, 2017 at 7:45
  • @RaphaelatDigitalPianism can you help on this? Commented Nov 3, 2017 at 6:50
  • @JagdishBarot any solution for this problem? Commented Feb 21, 2021 at 14:38

2 Answers 2

1

I assume you know how to write a plugin and how to write a script in Magento 2. With this in mind, the plan to have your issue resolved would be as follows:

  1. add a product attribute that is a boolean and define for all your products whether they can be displayed to guests or not —> make it layered enabled

  2. update all your products in your catalog with a script

  3. in the block \Magento\Catalog\Block\Product\ListProduct, you may change the function that loads the products using a plugin on the function getLoadedProductCollection

1
  • 1
    wrong product count in layernavigation. Commented Oct 16, 2018 at 7:56
1

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.

Your Answer

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

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