To modify product collection before rendering category page follow these steps:
Override Magento\Catalog\Block\Product\ListProduct
In function _getProductCollection after if statement insert your code:
$collection = $this->_productCollection; foreach ($collection as $k => $product){ // Here goes your code to modify product // For example:
$collection->removeItemByKey($k);// Remove original item from collection } $collection->setOrder('position','ASC');// Reorder collection as you want return $collection;
- Override Magento\Catalog\Block\Product\ListProduct 2. In function _getProductCollection after if statement insert your code:
$collection = $this->_productCollection;
foreach ($collection as $k => $product){
// Here goes your code to modify product
// For example:
$collection->removeItemByKey($k);// Remove original item from collection
}
$collection->setOrder('position','ASC');// Reorder collection as you want
return $collection;