I want to override protected method _prepareProduct present in vendor/magento/module-sitemap/Model/ResourceModel/Catalog/Product.php in my custom module.
I have overridden the Model file by adding preference in di.xml but it's not working.
Still, the default model is working. I have done di:compile and setup upgrade too. There is no error showing.
Please provide a solution to override this.
Thanks!
-
1can u write here your full code?Hafiz Arslan– Hafiz Arslan2019年09月25日 10:16:38 +00:00Commented Sep 25, 2019 at 10:16
-
@chanchal ,why you deleting the question again again?Amit Bera– Amit Bera ♦2019年09月25日 12:24:44 +00:00Commented Sep 25, 2019 at 12:24
-
@AmitBera Because the issue was there is a third party module present which has already overridden the same file. That's why it was not overriding mine. Now, I have overridden the third-party module file.chanchal– chanchal2019年09月25日 12:55:49 +00:00Commented Sep 25, 2019 at 12:55
1 Answer 1
Your di.xml code will be look like:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sitemap\Model\ResourceModel\Catalog\Product" type="{Vendorname}\{Modulename}\Rewrite\Magento\Sitemap\Model\ResourceModel\Catalog\Product"/>
</config>
And Rewrite class code:
<?php
namespace {Vendorname}\{Modulename}\Rewrite\Magento\Sitemap\Model\ResourceModel\Catalog;
class Product extends \Magento\Sitemap\Model\ResourceModel\Catalog\Product
{
protected function _prepareProduct(array $productRow, $storeId)
{
// Do changes at here
$product = new \Magento\Framework\DataObject();
$product['id'] = $productRow[$this->getIdFieldName()];
if (empty($productRow['url'])) {
$productRow['url'] = 'catalog/product/view/id/' . $product->getId();
}
$product->addData($productRow);
$this->_loadProductImages($product, $storeId);
return $product;
}
}
-
public function getCollection($storeId) Getting error in this functionJimit Bhavsar– Jimit Bhavsar2023年06月20日 17:46:47 +00:00Commented Jun 20, 2023 at 17:46
-
Notice: Undefined property: {{}}o\Sitemap\Model\ResourceModel\Catalog\Product::$connection inJimit Bhavsar– Jimit Bhavsar2023年06月20日 17:47:34 +00:00Commented Jun 20, 2023 at 17:47
-
Can you please help meJimit Bhavsar– Jimit Bhavsar2023年06月20日 17:48:27 +00:00Commented Jun 20, 2023 at 17:48
-
use Magento\Catalog\Helper\Product as HelperProduct; use Magento\Catalog\Model\Product\Image\UrlBuilder; use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; use Magento\Framework\App\ObjectManager; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\Store; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; class Product extends \Magento\Sitemap\Model\ResourceModel\Catalog\Product { }Jimit Bhavsar– Jimit Bhavsar2023年06月20日 17:49:12 +00:00Commented Jun 20, 2023 at 17:49
-
magento.stackexchange.com/questions/367897/…Jimit Bhavsar– Jimit Bhavsar2023年06月21日 01:12:33 +00:00Commented Jun 21, 2023 at 1:12
Explore related questions
See similar questions with these tags.