2

I am trying to override the _afterLoad methods of the vendor class Magento\Catalog\Model\ResourceModel\Product\Collection unsuccessfully.

In my custom module I edited the di.xml in this way

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" 
                type="SG\Utilities\Model\ResourceModel\Product\Collection" />
</config>

Then I created the file app/code/SG/Utilities/Model/ResourceModel/Product/Collection.php

<?php

namespace SG\Utilities\Model\ResourceModel\Product;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{

    protected function _afterLoad()
    {
        echo "called!";
        exit();
        if ($this->_addUrlRewrite) {
           $this->_addUrlRewrite();
        }

        $this->_prepareUrlDataObject();

        $this->_eventManager->dispatch('catalog_product_collection_load_after', ['collection' => $this]);

        return $this;
    }

}

but the _afterLoad method is never called. What am I doing wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.