Example #1
0
 /**
  * Generate XML file
  *
  * @see http://www.sitemaps.org/protocol.html
  *
  * @return $this
  */
 public function generateXml()
 {
     $this->_initSitemapItems();
     /** @var $sitemapItem \Magento\Framework\Object */
     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;
 }