Magento Asked by Amit Bera on December 5, 2020
I have rewrite catalog product resource collection MagentoCatalogModelResourceModelProductCollection
.
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
<?php
namespace AmitBeraCoreRewriteModelRewriteCatalogResourceModelProduct;
class Collection extends MagentoCatalogModelResourceModelProductCollection
{
protected $appState;
public function __construct(
MagentoFrameworkDataCollectionEntityFactory $entityFactory,
PsrLogLoggerInterface $logger,
MagentoFrameworkDataCollectionDbFetchStrategyInterface $fetchStrategy,
MagentoFrameworkEventManagerInterface $eventManager,
MagentoEavModelConfig $eavConfig,
MagentoFrameworkAppResourceConnection $resource,
MagentoEavModelEntityFactory $eavEntityFactory,
MagentoCatalogModelResourceModelHelper $resourceHelper,
MagentoFrameworkValidatorUniversalFactory $universalFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkModuleManager $moduleManager,
MagentoCatalogModelIndexerProductFlatState $catalogProductFlatState,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoCatalogModelProductOptionFactory $productOptionFactory,
MagentoCatalogModelResourceModelUrl $catalogUrl,
MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
MagentoCustomerModelSession $customerSession,
MagentoFrameworkStdlibDateTime $dateTime,
MagentoFrameworkAppState $appState,
MagentoCustomerApiGroupManagementInterface $groupManagement,
MagentoFrameworkDBAdapterAdapterInterface $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 = MagentoFrameworkDBSelect::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="MagentoCatalogModelResourceModelProductCollection"
type="AmitBeraCoreRewriteModelRewriteCatalogResourceModelProductCollection" />
</config>
Whenever use
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
/** @var MagentoCatalogModelResourceModelProductCollection $productCollection */
$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');
echo get_class($productCollection );
result:
AmitBeraCoreRewriteModelRewriteCatalogResourceModelProductCollectionInterceptor
echo get_class($productCollection );
give Interceptor classAmitBeraCoreRewriteModelRewriteCatalogResourceModelProductCollectionInterceptor
If you overwriting
MagentoCatalogModelResourceModelProductCollection
, 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 MagentoCatalogModelResourceModelProductCollectionProductLimitation
or using plugin concept to set $productLimitationFilters
Answered by Venkat on December 5, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP