I've added a field for my customer (called "Group").

I want to show the product from the catalog based on this group (I have added a "group" field in my product).

What do I need to override to do this ?

EDIT : based on answer I found a start for the solution, I'm overriding ListProduct.php but when I change the collection sort by adding addAttributeToFilter it shows the same result. See :

    protected function _getProductCollection()
    $group = $this->customerSession->getCustomer()->getData("groupe"); //NOT CUSTOMER GROUP, BUT A CUSTOM FIELD CALLED GROUP

    if ($this->_productCollection === null) {
        $this->_productCollection = $this->initializeProductCollection();
        try {
            $this->_productCollection->addAttributeToFilter("groupe_product", array('eq' => $group));
        } catch (LocalizedException $e) {
            echo $e;


    return $this->_productCollection;

Any thoughts ?

3 Answers 3


you can try this to filter the product based on customer groupId but remember you have to add customer group id to product

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class yourclass{

   protected $_customerSession;

   protected $_productCollection;

   public function __construct(

        \Magento\Customer\Model\Session $customerSession,
        CollectionFactory $collectionFactory,
    ) {
        $this->_customerSession = $customerSession;
        $this->_productCollection = $collectionFactory;


   public function getGroupId()
        if ($this->_customerSession->isLoggedIn()) {

            return $this->_customerSession->getCustomer()->getGroupId();

    public function yourfunction

        $groupId = $this->getGroupId();

       $collection = $this->_productCollection->create()
            ->addFieldToFilter('group_id', $groupId);

        // do your rest of work

  • What file should I override ? Mar 26, 2019 at 15:40
  • first add groupId to the productcollection and override the listproduct.php and let me if any issues
    – mohith
    Mar 27, 2019 at 4:27
  • Ended up to do something like that. Thanks ! Apr 1, 2019 at 15:39

We can't add customer based filters to ProductCollection in listing page. Collection will be cached and same collection will be served to all customers in PLP.

Adding customer based filters is not recommended.

You can achieve this by making listing page as non cacheable. Listing page loading time will be high.


You need to use product collection and filter products based on that customer group you created. Please do this in app/code .create a new module and can you please specify in which page you need to display that products.

  • On basic listing page and on search results. Mar 26, 2019 at 15:43
  • override list.phtml file in your theme and modify the collection in block, list.phtml is for listing page and search page . Please let me know if you have any questions.
    – Prasanth
    Mar 29, 2019 at 4:56

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.