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
Best Answer
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