0

I need to change the functionality of the generating sitemap xml. For that, I used around plugin for the generateXml() method in the Magento\Sitemap\Model\Sitemap class. When I try to generate the file, the filename to be generated is coming as empty. So it produces exceptional error. How can we resolve it?

my di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sitemap\Model\Sitemap">
        <plugin name="SitemapSplitter" type="Company\SitemapSplitter\Model\Sitemap" sortOrder="1"/>
    </type>
</config>

Company\SitemapSplitter\Model\Sitemap.php

<?php
namespace Company\SitemapSplitter\Model;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Model\AbstractModel;
class Sitemap extends \Magento\Sitemap\Model\Sitemap {

public function aroundGenerateXml(\Magento\Sitemap\Model\Sitemap $subject, \Closure $proceed, $requestInfo = null)
    {
      $this->_initSitemapItems();
        /** @var $sitemapItem \Magento\Framework\DataObject */
        foreach ($this->_sitemapItems as $sitemapItem) {
            $changefreq = $sitemapItem->getChangefreq();
            $priority = $sitemapItem->getPriority();
            foreach ($sitemapItem->getCollection() as $item) {
                $xml = $this->_getSitemapRow(
                    $item->getUrl(),
                    $item->getUpdatedAt(),
                    $changefreq,
                    $priority,
                    $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();
        }

        // Push sitemap to robots.txt
        if ($this->_isEnabledSubmissionRobots()) {
            $this->_addSitemapToRobotsTxt($this->getSitemapFilename());
        }

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

        return $this;
    }
}
?>
  • Please post your code – Keyur Shah Aug 22 '17 at 9:16
  • I have added the code now – SIBHI S Aug 22 '17 at 9:41
1

From the Magento\Sitemap\Model\Sitemap I can see that _initSitemapItems , _sitemapItems and many things are protected members and in plugin I think we can not access it by $this (and for public members we can access it by $subject object ). So I think you have to overwrite this method with the help of di.xml

  • Thanks Keyur! It's working fine by overwriting the class. – SIBHI S Aug 23 '17 at 12:08

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.