I have override \Magento\Catalog\Block\Adminhtml\Product\Widget\Chooser Block and I have added Store filter to Product Collection in _prepareCollection function. I am returning parent::_prepareCollection(); in my override block but it is not filtering my product as product wise. If I return $this->setCollection($collection); than it working fine but if I return parent::_prepareCollection(); than it does not filter my product collection.

Anyone have idea why this does not work when I return parent::_prepareCollection().

  • Can you please add code in question? – Rutvee Sojitra Aug 16 '18 at 6:44

As you can see in the parent block, the prepareCollection method creates new collection from factory and set it using setCollection method:

 * Prepare products collection, defined collection filters (category, product type)
 * @return Extended
protected function _prepareCollection()
    /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
    $collection = $this->_collectionFactory->create()->setStoreId(0)->addAttributeToSelect('name');

    if ($categoryId = $this->getCategoryId()) {
        $category = $this->_categoryFactory->create()->load($categoryId);
        if ($category->getId()) {
            // $collection->addCategoryFilter($category);
            $productIds = $category->getProductsPosition();
            $productIds = array_keys($productIds);
            if (empty($productIds)) {
                $productIds = 0;
            $collection->addFieldToFilter('entity_id', ['in' => $productIds]);

    if ($productTypeId = $this->getProductTypeId()) {
        $collection->addAttributeToFilter('type_id', $productTypeId);

    return parent::_prepareCollection();

So, if you adding the store id filter to the collection and than call a parent method it will overwrite you collection, and by this way your filters has gone.

Better way will be first call the parent method, then add your filters and call the setCollection method one more time.

It will be look like this:

protected function _prepareCollection()
    // Create collection in general way

    // Get this collection
    $collection = $this->getCollection();

    // Add your filter here
    // Add your filter here
    // Add your filter here

    // Update stored collection

    return $this;

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.