cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2 rewrite: product resource collection class does not effect on functions

Magento2 rewrite: product resource collection class does not effect on functions

I have rewrite catalog product resource collection Magento\Catalog\Model\ResourceModel\Product\Collection.

 Class override successfully but functions override is not working.

Suppose, i want override setOrder()function. But it does not work. Always calling Core function instead of rewrite class function..

 

<?php
namespace AmitBera\CoreRewrite\Model\Rewrite\Catalog\ResourceModel\Product;

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

    protected $appState;
    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\Framework\App\State $appState,
        \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
            );
        $this->_appState = $appState;

    }
   public function setOrder($attribute, $dir = \Magento\Framework\DB\Select::SQL_DESC)
    {
        echo __METHOD__;
        exit;
        if ($attribute == 'price') {
            $this->addAttributeToSort($attribute, $dir);
        } else {
            parent::setOrder($attribute, $dir);
        }
        return $this;
    }


}

module.xml

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="AmitBera_CoreRewrite" setup_version="2.0.0">
        <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>     
    </module>
</config>

di.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="AmitBera\CoreRewrite\Model\Rewrite\Catalog\ResourceModel\Product\Collection" />
</config>   

 

Whenever use

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');echo get_class($productCollection );

Result : AmitBera\CoreRewrite\Model\Rewrite\Catalog\ResourceModel\Product\Collection\Interceptor

Question:

i donot understand why i am not able to override function setorder()

Why

echo get_class($productCollection ); 

give Interceptor class

 

Tags (2)
1 REPLY

Re: Magento2 rewrite: product resource collection class does not effect on functions

If you want to override the ( setOrder()) kindly use the plugin instead of overriding the model class

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
            <type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
            <plugin name="tesultchange" type="Test\Test\Model\Resultchange" sortOrder="10" /></type>
    </config>

 

Resultchange.php

use after or before

<?php
    
    namespace Test\Test\Model;
    
    class Resultchange {
    
        
        public function aftersetOrder()
        {
           //do your thing
        }
    }



Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti