Skip to content

Magento 2 override product list page collection

I am trying to override the product collection of the product list page (category page).
(Just for testing locally) I am editing the vendor/magento/module-catalog/Block/Product/ListProduct.php file and the _beforeToHtml() method:

protected function _beforeToHtml()
{
    $collection = $this->_getProductCollection();
    $collection->addAttributeToSelect('*');
    $collection->addAttributeToFilter('manufacturer', array('eq' => 60491));

    echo $collection->getSelect();
    .
    .
    .
}

When I paste the output of $collection->getSelect() in the my DB it returns the correct products, but on the frontend it shows 0 products.
Any idea why this is? Thanks

Magento 2.4.5

EDIT:
I have also tried using a aroundPrepareProductCollection plugin:

    <type name="MagentoCatalogModelLayer">
    <plugin name="extend_layer" type="SulmanAllowedBrandsPluginLayerPlugin" sortOrder="1"/>
</type>

But it does the same (SQL returns correct results – but nothing displayed on frontend)