Skip to content
This repository was archived by the owner on Jul 23, 2025. It is now read-only.

Commit 85f20f3

Browse files
authored
Merge pull request #1 from Vendic/develop
Develop
2 parents d02ab4c + 02f0b5e commit 85f20f3

15 files changed

+673
-1
lines changed

Cron/GenerateSitemap.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @author tjitse (Vendic)
6+
* Created on 22/03/2019 15:31
7+
*/
8+
9+
namespace Vendic\VueStorefrontSitemap\Cron;
10+
11+
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Catalog\Api\Data\ProductInterface;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use SitemapPHP\Sitemap;
15+
use Vendic\VueStorefrontSitemap\Model\CategoryCollection;
16+
use Vendic\VueStorefrontSitemap\Model\Configuration;
17+
use Vendic\VueStorefrontSitemap\Model\ProductCollection;
18+
use Vendic\VueStorefrontSitemap\Model\SitemapFactory;
19+
20+
class GenerateSitemap
21+
{
22+
const SITEMAP_NAME = 'sitemap.xml';
23+
24+
/**
25+
* @var DirectoryList
26+
*/
27+
protected $directoryList;
28+
/**
29+
* @var SitemapFactory
30+
*/
31+
protected $sitemapFactory;
32+
/**
33+
* @var ProductCollection
34+
*/
35+
protected $productCollection;
36+
/**
37+
* @var Sitemap
38+
*/
39+
protected $sitemap;
40+
/**
41+
* @var Configuration
42+
*/
43+
protected $configuration;
44+
/**
45+
* @var CategoryCollection
46+
*/
47+
protected $categoryCollection;
48+
49+
public function __construct(
50+
CategoryCollection $categoryCollection,
51+
Configuration $configuration,
52+
ProductCollection $productCollection,
53+
DirectoryList $directoryList,
54+
SitemapFactory $sitemapFactory
55+
) {
56+
$this->directoryList = $directoryList;
57+
$this->sitemapFactory = $sitemapFactory;
58+
$this->productCollection = $productCollection;
59+
$this->configuration = $configuration;
60+
$this->categoryCollection = $categoryCollection;
61+
}
62+
63+
public function execute() : void
64+
{
65+
// Collect settings
66+
$domain = $this->configuration->getVueStorefrontUrl();
67+
$path = $this->getPubPath();
68+
69+
// Sitemap configuration
70+
$this->sitemap = $this->sitemapFactory->create($domain);
71+
$this->sitemap->setPath($path);
72+
$this->sitemap->setFilename('sitemap');
73+
74+
// Add data
75+
$this->addHomepageToSitemap();
76+
$this->addCategoriesToSitemap();
77+
$this->addProductsToSitemap();
78+
79+
// Generate
80+
$this->sitemap->createSitemapIndex($domain, 'Today');
81+
}
82+
83+
/**
84+
* @throws \Magento\Framework\Exception\FileSystemException
85+
*/
86+
private function getPubPath() : string
87+
{
88+
return $this->directoryList->getPath('pub') . '/';
89+
}
90+
91+
/**
92+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
93+
*/
94+
private function getActiveProducts()
95+
{
96+
return $this->productCollection->get();
97+
}
98+
99+
/**
100+
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
101+
*/
102+
private function getActiveCategories()
103+
{
104+
return $this->categoryCollection->get();
105+
}
106+
107+
protected function addProductsToSitemap(): void
108+
{
109+
$activeProducts = $this->getActiveProducts();
110+
if ($activeProducts->count() >= 1) {
111+
/** @var ProductInterface $product */
112+
foreach ($activeProducts->getItems() as $product) {
113+
$productUrl = '/' . $product->getSku() . '/' . $product->getUrlKey();
114+
$this->sitemap->addItem(
115+
$productUrl,
116+
1.0,
117+
'daily',
118+
$product->getUpdatedAt()
119+
);
120+
}
121+
}
122+
}
123+
124+
protected function addCategoriesToSitemap(): void
125+
{
126+
$activeCategories = $this->getActiveCategories();
127+
if ($activeCategories->count() >= 1) {
128+
/** @var CategoryInterface $category */
129+
foreach ($activeCategories->getItems() as $category) {
130+
$categoryUrl = '/' . $category->getUrlKey() . '-' . $category->getId();
131+
$this->sitemap->addItem(
132+
$categoryUrl,
133+
1.0,
134+
'daily',
135+
$category->getUpdatedAt()
136+
);
137+
}
138+
}
139+
}
140+
141+
protected function addHomepageToSitemap() : void
142+
{
143+
$this->sitemap->addItem(
144+
'/'
145+
);
146+
}
147+
}

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2019 Vendic B.V.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Model/CategoryCollection.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @author tjitse (Vendic)
6+
* Created on 26/03/2019 09:43
7+
*/
8+
9+
namespace Vendic\VueStorefrontSitemap\Model;
10+
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
12+
13+
class CategoryCollection
14+
{
15+
/**
16+
* @var CollectionFactory
17+
*/
18+
protected $collectionFactory;
19+
20+
public function __construct(
21+
CollectionFactory $collectionFactory
22+
) {
23+
$this->collectionFactory = $collectionFactory;
24+
}
25+
26+
/**
27+
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
28+
*/
29+
public function get()
30+
{
31+
$collection = $this->collectionFactory->create();
32+
$collection->addFieldToSelect('url_key');
33+
$collection->addFieldToSelect('level');
34+
$collection->addFieldToFilter('url_key', ['neq' => null]);
35+
36+
return $collection;
37+
}
38+
}

Model/Configuration.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @author tjitse (Vendic)
6+
* Created on 23/03/2019 10:33
7+
*/
8+
9+
namespace Vendic\VueStorefrontSitemap\Model;
10+
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
14+
class Configuration
15+
{
16+
const VUE_STOREFRONT_URL_CONFIG_PATH = 'vuestorefront/sitemap/vs_url';
17+
18+
/**
19+
* @var ScopeConfigInterface
20+
*/
21+
protected $scopeConfig;
22+
23+
public function __construct(
24+
ScopeConfigInterface $scopeConfig
25+
) {
26+
$this->scopeConfig = $scopeConfig;
27+
}
28+
29+
public function getVueStorefrontUrl(): string
30+
{
31+
$url = $this->scopeConfig->getValue(
32+
self::VUE_STOREFRONT_URL_CONFIG_PATH,
33+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
34+
);
35+
if (!is_string($url)) {
36+
throw new LocalizedException(
37+
__('Invalid or no VueStorefront url entered for config path %1', self::VUE_STOREFRONT_URL_CONFIG_PATH)
38+
);
39+
}
40+
return $url;
41+
}
42+
}

Model/ProductCollection.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @author tjitse (Vendic)
6+
* Created on 22/03/2019 18:39
7+
*/
8+
9+
namespace Vendic\VueStorefrontSitemap\Model;
10+
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Catalog\Model\Product\Visibility;
13+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
class ProductCollection
17+
{
18+
19+
/**
20+
* @var CollectionFactory
21+
*/
22+
protected $productCollectionFactory;
23+
/**
24+
* @var Status
25+
*/
26+
protected $productStatus;
27+
/**
28+
* @var Visibility
29+
*/
30+
protected $productVisibility;
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
protected $storeManager;
35+
36+
public function __construct(
37+
StoreManagerInterface $storeManager,
38+
Status $productStatus,
39+
CollectionFactory $productCollectionFactory,
40+
Visibility $productVisibility
41+
) {
42+
$this->productCollectionFactory = $productCollectionFactory;
43+
$this->productStatus = $productStatus;
44+
$this->productVisibility = $productVisibility;
45+
$this->storeManager = $storeManager;
46+
}
47+
48+
/**
49+
* Get active products
50+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
51+
*/
52+
public function get()
53+
{
54+
$collection = $this->productCollectionFactory->create();
55+
// We need to add store filters first to avoid an Zend_Db_Statement_Exception https://github.com/magento/magento2/issues/15187
56+
$collection->addStoreFilter($this->getDefaultStoreId());
57+
$collection->addAttributeToSelect('url_key');
58+
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
59+
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
60+
61+
return $collection;
62+
}
63+
64+
/**
65+
* TODO make store ID configurable via Magento backend
66+
* @return int
67+
*/
68+
private function getDefaultStoreId()
69+
{
70+
return $this->storeManager->getDefaultStoreView()->getId();
71+
}
72+
}

Model/SitemapFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @author tjitse (Vendic)
6+
* Created on 22/03/2019 18:07
7+
*/
8+
9+
namespace Vendic\VueStorefrontSitemap\Model;
10+
11+
use SitemapPHP\Sitemap;
12+
13+
class SitemapFactory
14+
{
15+
public function create(string $domain) : Sitemap
16+
{
17+
return new Sitemap($domain);
18+
}
19+
}

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
# VueStorefront XML sitemap generator for Magento 2
2+
**Note: this package isn't production ready. Please test thoroughly before using in production**
3+
4+
This modules generates a sitemap xml (via cron job, everyday at 00:00) for VueStorefront projects that are integrated with Magento 2. VueStorefront uses a special url structure, based on Magento 2 data:
5+
6+
**Categories:**
7+
8+
https://vuestorefronturl.com/urlkey-id
9+
10+
**Products:**
11+
12+
https://vuestorefronturl.com/sku/urlkey
13+
14+
## Installation
15+
```
16+
composer require composer require vendic/magento2-vuestorefront-xmlsitemap
17+
```
18+
19+
Go to system configuration:
20+
> Stores > Configuration > Vendic > VueStorefront
21+
22+
And set your Vuestorefront homepage url.
23+
24+
### Contributors
25+
[Tjitse Efde](https://vendic.nl)
26+
27+
### License
28+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

0 commit comments

Comments
 (0)