2

I want to override the model class(Magento\Catalog\Model\ResourceModel\Product\Collection.php)

Flow the below process to override the addUrlRewrite method but not working.

di.xml

<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="MyModule\Custom\Model\ResourceModel\Product\Collection" />
</config>

Collection.php

<?php
    namespace MyModule\Custom\Model\ResourceModel\Product;

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

        public function __construct(
            \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
            \Psr\Log\LoggerInterface $logger,
            \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
            \Magento\Framework\Event\ManagerInterface $eventManager,
            \Magento\Eav\Model\Config $eavConfig,
            \Magento\Framework\App\ResourceConnection $resource,
            \Magento\Eav\Model\EntityFactory $eavEntityFactory,
            \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
            \Magento\Framework\Validator\UniversalFactory $universalFactory,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\Module\Manager $moduleManager,
            \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
            \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
            \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
            \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
            \Magento\Customer\Model\Session $customerSession,
            \Magento\Framework\Stdlib\DateTime $dateTime,
            \Magento\Customer\Api\GroupManagementInterface $groupManagement,
            \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
        ) {
            parent::__construct(
                    $entityFactory,
                    $logger,
                    $fetchStrategy,
                    $eventManager,
                    $eavConfig,
                    $resource,
                    $eavEntityFactory,
                    $resourceHelper,
                    $universalFactory,
                    $storeManager,
                    $moduleManager,
                    $catalogProductFlatState,
                    $scopeConfig,
                    $productOptionFactory,
                    $catalogUrl,
                    $localeDate,
                    $customerSession,
                    $dateTime,
                    $groupManagement,
                            $connection
                );


        }
         public function addUrlRewrite($categoryId = '')
        {
            echo '<pre>';exit(var_dump("Hello Test"));
        }



    }

Please, can you help on above issue? any references and suggestion are highly appreciated.

2
  • If you want to modify any functions in the sense then you don't need constructor unless you want any class to inject and used anywhere in your code @Parthavi May 24, 2019 at 5:27
  • What you want to achieve by overriding this method? May 24, 2019 at 5:35

1 Answer 1

0

In module.xml add this :-

<sequence>
   <module name="Magento_Catalog"/> 
   <module name="Magento_CatalogSearch"/>         
   <module name="Magento_Checkout"/>
   <module name="Magento_Sales"/>
   <module name="Magento_Quote"/>
   <module name="Magento_Vault"/>
   <module name="Magento_OfflineShipping"/>           
</sequence> 

Or you Can Use Plugin For Override addUrlRewrite function

put below code in Namespace/Module/etc/di.xml

<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
    <plugin name="product-collection-addurlrewrite" type="Namespace\Module\Plugin\Product\Collection" sortOrder="1"/>
</type>

Create Namespace\Module\Plugin\Product\Collection.php and pasted below code in your file:

<?php
namespace Namespace\Module\Plugin\Product;

class Collection
{

    public function (typeofplugin(after,before,around))addUrlRewrite(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject)
    {
         // do your stuff here
        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.