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);
}
$this->setCollection($collection);
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
parent::_prepareCollection();
// Get this collection
$collection = $this->getCollection();
// Add your filter here
// Add your filter here
// Add your filter here
// Update stored collection
$this->setCollection($collection);
return $this;
}