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