1

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!

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

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;
    }
}

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.