appEmulation = $appEmulation ?: ObjectManager::getInstance() ->get(\Magento\Store\Model\App\Emulation::class); } /** * Generate sitemap * * @return void */ public function execute() { // init and load sitemap model $id = $this->getRequest()->getParam('sitemap_id'); $sitemap = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class); /* @var $sitemap \Magento\Sitemap\Model\Sitemap */ $sitemap->load($id); // if sitemap record exists if ($sitemap->getId()) { try { //We need to emulate to get the correct frontend URL for the product images $this->appEmulation->startEnvironmentEmulation( $sitemap->getStoreId(), \Magento\Framework\App\Area::AREA_FRONTEND, true ); $sitemap->generateXml(); $this->messageManager->addSuccessMessage( __('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename()) ); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.')); } finally { $this->appEmulation->stopEnvironmentEmulation(); } } else { $this->messageManager->addErrorMessage(__('We can\'t find a sitemap to generate.')); } // go to grid $this->_redirect('adminhtml/*/'); } }