I trying to figure out how to modify some rows in product collection before it will be rendered at category page. I'm digging around getProductListHtml() (module-catalog/Block/Category/View.php) but no success.

  • You can maybe use the observer catalog_block_product_list_collection ? Jul 25 '17 at 15:39

To modify product collection before rendering category page follow these steps:

1) 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:
   $newName = $product->getName().' (HOT SALE)';// just example guys
   $collection->addItem($product);// Add modified item to collection
   $collection->removeItemByKey($k);// Remove original item from collection 
$collection->setOrder('position','ASC');// Reorder collection as you want
  • 1
    It's always best to use a Plugin as much as possible. Only use Preference if there's no other choice. For this, you can just even use an after Plugin to manipulate the result of the getLoadedProductCollection method.
    – phagento
    May 21 at 5:47

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.