1

I'm trying to override core Model Magento\Sitemap\Model\Sitemap with my own model My\Namespace\Model\Sitemap as follows,

<preference for="Magento\Sitemap\Model\Sitemap"    type="My\Namespace\Model\Sitemap" />

All other my overridden Models and Blocks are working fine, but for some strange reason, this override for Sitemap Model does not work,

My custom Model is almost same with only few changes inside of few methods.

<?php

namespace My\Namespace\Model;

class Sitemap extends \Magento\Sitemap\Model\Sitemap
{
    protected function _getSitemapRow($url, $lastmod = null, $changefreq = null, $priority = null, $images = null)
    {
        $url = $this->_getUrl($url);
        $row = '<locS>' . htmlspecialchars( $url ) . '</locS>';
        if ($lastmod) {
            $row .= '<lastmod>' . $this->_getFormattedLastmodDate($lastmod) . '</lastmod>';
        }
        if ($changefreq) {
            $row .= '<changefreq>' . $changefreq . '</changefreq>';
        }
        if ($priority) {
            $row .= sprintf('<priority>%.1f</priority>', $priority);
        }
        if ($images) {
            foreach ($images->getCollection() as $image) {
                $row .= '<image:image>';
                $row .= '<image:loc>' . htmlspecialchars($this->_getMediaUrl($image->getUrl())) . '</image:loc>';
                $row .= '<image:title>' . htmlspecialchars($images->getTitle()) . '</image:title>';
                if ($image->getCaption()) {
                    $row .= '<image:caption>' . htmlspecialchars($image->getCaption()) . '</image:caption>';
                }
                $row .= '</image:image>';
            }
            $row .= '<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0"><DataObject type="thumbnail">';
            $row .= '<Attribute name="name" value="' . htmlspecialchars($images->getTitle()) . '"/>';
            $row .= '<Attribute name="src" value="' . htmlspecialchars(
                $this->_getMediaUrl($images->getThumbnail())
            ) . '"/>';
            $row .= '</DataObject></PageMap>';
        }

        return '<url>' . $row . '</url>';
    }

    protected function _getSitemapIndexRow($sitemapFilename, $lastmod = null)
    {
        $url = $this->getSitemapUrl($this->getSitemapPath(), $sitemapFilename);
        $row = '<locS>' . htmlspecialchars( $url ) . '</locS>';
        if ($lastmod) {
            $row .= '<lastmod>' . $this->_getFormattedLastmodDate($lastmod) . '</lastmod>';
        }

        return '<sitemap>' . $row . '</sitemap>';
    }
}
  • Your overridden model should work, we need to clear cache, var generation. And try again. – Khoa TruongDinh Dec 20 '16 at 4:48
  • show your custom model code – Rakesh Jesadiya Dec 20 '16 at 5:12
  • added the code to the question – nuwaus Dec 20 '16 at 5:50
  • Can you please try to make your function public instead of private & check? If it dosen't work copy whole class & add ur changes & check class Sitemap extends \Magento\Sitemap\Model\Sitemap remains Same – Ankit Shah Dec 20 '16 at 5:53
  • does not work, but to override I think the method should have the same access modifier. in this case ` protected` – nuwaus Dec 20 '16 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.