diff --git a/Console/RunGenerateSitemap.php b/Console/RunGenerateSitemap.php index 3e40e66..f1972cf 100644 --- a/Console/RunGenerateSitemap.php +++ b/Console/RunGenerateSitemap.php @@ -13,10 +13,14 @@ class RunGenerateSitemap extends Command */ protected $generateSitemapCron; + protected $state; + public function __construct( + \Magento\Framework\App\State $state, GenerateSitemap $generateSitemapCron ) { $this->generateSitemapCron = $generateSitemapCron; + $this->state = $state; parent::__construct(); } protected function configure() @@ -28,7 +32,7 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { try { - $this->generateSitemapCron->execute(); + $this->state->emulateAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND, [$this->generateSitemapCron, 'execute']); $output->writeln("VSF Sitemap Generated"); } catch (\Exception $e) { $output->writeln($e->getMessage()); diff --git a/Cron/GenerateSitemap.php b/Cron/GenerateSitemap.php index 51217cb..8716e8e 100644 --- a/Cron/GenerateSitemap.php +++ b/Cron/GenerateSitemap.php @@ -11,6 +11,8 @@ use Magento\Catalog\Api\Data\CategoryInterface; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Store\Model\App\Emulation; +use Magento\Store\Model\StoreManagerInterface; use SitemapPHP\Sitemap; use Vendic\VueStorefrontSitemap\Model\CategoryCollection; use Vendic\VueStorefrontSitemap\Model\Configuration; @@ -47,11 +49,23 @@ class GenerateSitemap */ protected $categoryCollection; /** - * @var File + * @var StoreManagerInterface */ protected $fileDriver; + /** + * @var StoreManagerInterface + */ + protected $storeManager; + + /** + * @var Emulation + */ + protected $storeEmulation; + public function __construct( + Emulation $storeEmulation, + StoreManagerInterface $storeManager, CategoryCollection $categoryCollection, Configuration $configuration, ProductCollection $productCollection, @@ -64,30 +78,39 @@ public function __construct( $this->productCollection = $productCollection; $this->configuration = $configuration; $this->categoryCollection = $categoryCollection; - $this->fileDriver = $fileDriver; + $this->fileDriver = $fileDriver; + $this->storeManager = $storeManager; + $this->storeEmulation = $storeEmulation; } public function execute() : void { - // Collect settings - $domain = $this->configuration->getVueStorefrontUrl(); - $path = $this->getPubPath(); + foreach ($this->storeManager->getStores() as $store) { + $this->storeEmulation->startEnvironmentEmulation($store->getId(),\Magento\Framework\App\Area::AREA_FRONTEND, true); + // Collect settings + $domain = $this->configuration->getVueStorefrontUrl(); + $path = $this->getPubPath(); // Create directory at Path if doesn't exists - if (!$this->fileDriver->isDirectory($path)) $this->fileDriver->createDirectory($path, 0775); - + if (!$this->fileDriver->isDirectory($path)) { + $this->fileDriver->createDirectory($path, 0775); + } + // Sitemap configuration $this->sitemap = $this->sitemapFactory->create($domain); $this->sitemap->setPath($path); $this->sitemap->setFilename('sitemap'); - // Add data - $this->addHomepageToSitemap(); - $this->addCategoriesToSitemap(); - $this->addProductsToSitemap(); + // Add data + $this->addHomepageToSitemap(); + $this->addCategoriesToSitemap(); + $this->addProductsToSitemap(); - // Generate - $this->sitemap->createSitemapIndex($domain, 'Today'); + // Generate + $this->sitemap->createSitemapIndex($domain, 'Today'); + + $this->storeEmulation->stopEnvironmentEmulation(); + } } /** diff --git a/Model/Configuration.php b/Model/Configuration.php index e7a4c51..57371c9 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -19,6 +19,7 @@ class Configuration const VUE_STOREFRONT_EXCLUDE_PRODUCT_SKUS_ENABLED = 'vuestorefront/sitemap/exclude_product_skus'; const VUE_STOREFRONT_SITEMAP_FOLDER = 'vuestorefront/sitemap/sitemap_folder'; + const VUE_STOREFRONT_SITEMAP_FILE = 'vuestorefront/sitemap/sitemap_file'; const VUE_STOREFRONT_CATEOGRY_URL_PATH_ENABLED = 'vuestorefront/sitemap/category_url_path'; @@ -86,6 +87,15 @@ public function getVueStorefrontSitemapFolder(): string return $folder; } + public function getVueStorefrontSitemapFilename(): string + { + $file = $this->scopeConfig->getValue(self::VUE_STOREFRONT_SITEMAP_FILE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); + if (!is_string($file)) { + $folder = 'sitemap'; + } + return $file; + } + public function getVueStorefrontCategoryUrlPath(): bool { $setting = $this->scopeConfig->getValue( diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index ba211d3..3a70a77 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -18,6 +18,10 @@ Path in /pub/ + + + without .xml file extension + Magento\Config\Model\Config\Source\Yesno