3

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 overrider setOrder()function. But it does not works . Always calling Core function instead of rewrite class function

Class

<?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

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

  • I am facing the same issue when I try to override addPriceData() method – Maniprakash Chinnasamy Feb 16 '18 at 13:31
  • I am facing same issue when i try to override function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC) and function _productLimitationPrice($joinLeft = false) method. Please give this solution. – Payal Patel Jun 12 '18 at 10:53
0

If you overwriting Magento\Catalog\Model\ResourceModel\Product\Collection, just traverse this folder location, you will find the file and folder with same name i.e Collection. So its points to folder location collection.

If you want to do some function with sorting overwrite Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation or using plugin concept to set $productLimitationFilters

| improve this answer | |

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.