I have a block where I want 3 different collections with different category filters. I have created collection object in constructor like this:

protected $collection;
public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
    $this->collection = $collection;

This is my function where I am getting collection

public function getMyCollection($categories)
    $collection = $this->collection;


    //Enable filter
        'status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)


    $collection->addCategoriesFilter(array('in' => $categories));

    //Setting up pagination
    //echo $collection->getSelect();

    return $collection->load();

The problem that I am facing is, when I call this function for first time, it runs properly, if I call it 2nd time, then it is not refreshing previous filters and adding new filters along with previous ones.

like categories in(23,24) AND categroies in(25,26)

How can I get multiple collections with single object with different filters everytime?

  • did you try using Magento\Catalog\Model\ResourceModel\Product\CollectionFactory ?
    – fmsthird
    May 9 '19 at 4:59
  • is Magento\Catalog\Model\ResourceModel\Product\Collection wrong? May 9 '19 at 5:00
  • nah, I don't think so. But maybe it is better to use a collection factory instead? maybe that could be the reason for the issue?
    – fmsthird
    May 9 '19 at 5:02
  • No @magefms actually collection initialized one time, and if you apply more and more filter to any collection that it will append in that collection not reinitialize collection everytime, so it doesn't matter if I use CollectionFactory or Collection, both will be same May 9 '19 at 5:05

You have to use collection Factory

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,

$this->collectionFactory= $collectionFactory


because this will return every time new instance and no filter will apply default.

  • worked for me. Thanks :) May 9 '19 at 10:38

Your Answer

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

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