In our Magento project we have around 200 thousand skus and many of them are with incomplete information. So we made a filter to prevent products without images from appearing on the category pages.
<catalog_product_collection_load_before>
<observers>
<mymodel_catalog_product_collection_load_before_clean_images>
<type>singleton</type>
<class>mymodel/observer</class>
<method>filterProductsWithoutImages</method>
</mymodel_catalog_product_collection_load_before_clean_images>
</observers>
</catalog_product_collection_load_before>
...
public function filterProductsWithoutImages($observer) {
if (isset($observer['collection'])) {
$collection = $observer['collection'];
$collection->addAttributeToFilter('small_image', array('neq' => "no_selection"));
return $this;
}
}
Products without images are removed from the collection. The problem occurs with pagination. The filtered products generate "holes" in the product listing. Example:
Category Accessories: 100 SKUS active.
Page 1: 36 products. Only 20 products are displayed, because 16 are without images.
Page 2: 36 products. Only 9 products are displayed, because 27 are without images.
Page 3: 28 products. The 28 products are displayed because they all have images.
So the problem is that after changing the collection, either through events catalog_product_collection_load_before
or catalog_product_collection_load_after
, the collection's pagination is not updated and the indices of the filtered products remain.
My questions:
In my understanding, the page was built after the load of the collection (so I was using the catalog_product_collection_load_before event). But because of this situation, it does not seem to be that way. How does it actually work?
Considering that if I edit the collection and load it again it can generate an infinite loop, how can I update the collection so as not to generate these "holes"?