0

I have used the preference to override the Sitemap model. Working fine for 4 months.

I have also tried to find other module is overriding the same but didn't get anything.

Can somebody help me to find out the issue?

<?php

namespace VendorName\ModuleName\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

class Sitemap extends \Magento\Sitemap\Model\Sitemap
{

    public function getCustomurlCollection()
    {
        $siteMapcollection = array();
        $sitecollection  = array();
        /* if want to add multiple url then load your custom collection and set  */

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $siteMapcollection = array('customurl','customurl','customurl','blog','franchise','contact','customurl','customurl');

        $sitemapItemFactoryobj = $objectManager->get(\Magento\Sitemap\Model\SitemapItemInterfaceFactory::class);
           foreach ($siteMapcollection as $custom) { 
                    $sitecollection[] = $sitemapItemFactoryobj->create([
                        'id'         => $custom,
                        'url'        => $custom,
                        'changeFrequency' => 'daily',
                        'updatedAt' => date("Y-m-d h:i:s"),
                        'priority' => '0.5',
                    ]);
                  }            
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/sitemap.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('sitemap custome url below');
        $logger->info($sitecollection);
            return $sitecollection;

    }

    public function getcustomHomePageUrl()
    {
        $sitecollectionhp = array();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $sitemapItemFactoryobjhp = $objectManager->get(\Magento\Sitemap\Model\SitemapItemInterfaceFactory::class);
        $sitecollectionhp[] = $sitemapItemFactoryobjhp->create([
                        'id'         => 'homepage',
                        'url'        =>  '',
                        'changeFrequency' => 'daily',
                        'updatedAt' => date("Y-m-d h:i:s"),
                        'priority' => '0.1',
                    ]);
        return $sitecollectionhp;
    }

    public function generateXml()
    {

        $this->_initSitemapItems();
        $customHomepageurl = $this->getcustomHomePageUrl();
        $customCollection = $this->getCustomurlCollection();
        $mergecustom = array_merge($customHomepageurl,$this->_sitemapItems);
        $completeCollection = array_merge($mergecustom,$customCollection);

        /** @var $item SitemapItemInterface */
        foreach ($completeCollection as $item) {
            $xml = $this->_getSitemapRow(
                $item->getUrl(),
                $item->getUpdatedAt(),
                $item->getChangeFrequency(),
                $item->getPriority(),
                $item->getImages()
            );

            if ($this->_isSplitRequired($xml) && $this->_sitemapIncrement > 0) {
                $this->_finalizeSitemap();
            }

            if (!$this->_fileSize) {
                $this->_createSitemap();
            }

            $this->_writeSitemapRow($xml);
            // Increase counters
            $this->_lineCount++;
            $this->_fileSize += strlen($xml);
        }

        $this->_finalizeSitemap();

        if ($this->_sitemapIncrement == 1) {
            // In case when only one increment file was created use it as default sitemap
            $path = rtrim(
                $this->getSitemapPath(),
                '/'
            ) . '/' . $this->_getCurrentSitemapFilename(
                $this->_sitemapIncrement
            );
            $destination = rtrim($this->getSitemapPath(), '/') . '/' . $this->getSitemapFilename();

            $this->_directory->renameFile($path, $destination);
        } else {
            // Otherwise create index file with list of generated sitemaps
            $this->_createSitemapIndex();
        }

        $this->setSitemapTime($this->_dateModel->gmtDate('Y-m-d H:i:s'));
        $this->save();

        return $this;
    }
}
3
  • i think the issue with URL rewrites
    – Amit Bera
    Feb 28, 2021 at 15:03
  • @AmitBera above id my code this way I am adding the custom URL in the sitemap. And by using amasty plugin to create a feed sitemap xml
    – Mahendra
    Feb 28, 2021 at 18:08
  • If am creting the sitemap from backend menu Marketting->SEO->Sitemap The custom urls are added this working here
    – Mahendra
    Feb 28, 2021 at 18:20

0

Your Answer

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