2

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!

3
  • 1
    can u write here your full code? Commented Sep 25, 2019 at 10:16
  • @chanchal ,why you deleting the question again again?
    – Amit Bera
    Commented 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
    Commented Sep 25, 2019 at 12:55

1 Answer 1

2

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;
    }
}
5
  • public function getCollection($storeId) Getting error in this function Commented Jun 20, 2023 at 17:46
  • Notice: Undefined property: {{}}o\Sitemap\Model\ResourceModel\Catalog\Product::$connection in Commented Jun 20, 2023 at 17:47
  • Can you please help me Commented 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 { } Commented Jun 20, 2023 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.