Skip to content

Commit

Permalink
Merge pull request #1 from Vendic/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Tjitse-E committed Mar 26, 2019
2 parents d02ab4c + 02f0b5e commit 85f20f3
Show file tree
Hide file tree
Showing 15 changed files with 673 additions and 1 deletion.
147 changes: 147 additions & 0 deletions Cron/GenerateSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
declare(strict_types=1);

/**
* @author tjitse (Vendic)
* Created on 22/03/2019 15:31
*/

namespace Vendic\VueStorefrontSitemap\Cron;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use SitemapPHP\Sitemap;
use Vendic\VueStorefrontSitemap\Model\CategoryCollection;
use Vendic\VueStorefrontSitemap\Model\Configuration;
use Vendic\VueStorefrontSitemap\Model\ProductCollection;
use Vendic\VueStorefrontSitemap\Model\SitemapFactory;

class GenerateSitemap
{
const SITEMAP_NAME = 'sitemap.xml';

/**
* @var DirectoryList
*/
protected $directoryList;
/**
* @var SitemapFactory
*/
protected $sitemapFactory;
/**
* @var ProductCollection
*/
protected $productCollection;
/**
* @var Sitemap
*/
protected $sitemap;
/**
* @var Configuration
*/
protected $configuration;
/**
* @var CategoryCollection
*/
protected $categoryCollection;

public function __construct(
CategoryCollection $categoryCollection,
Configuration $configuration,
ProductCollection $productCollection,
DirectoryList $directoryList,
SitemapFactory $sitemapFactory
) {
$this->directoryList = $directoryList;
$this->sitemapFactory = $sitemapFactory;
$this->productCollection = $productCollection;
$this->configuration = $configuration;
$this->categoryCollection = $categoryCollection;
}

public function execute() : void
{
// Collect settings
$domain = $this->configuration->getVueStorefrontUrl();
$path = $this->getPubPath();

// Sitemap configuration
$this->sitemap = $this->sitemapFactory->create($domain);
$this->sitemap->setPath($path);
$this->sitemap->setFilename('sitemap');

// Add data
$this->addHomepageToSitemap();
$this->addCategoriesToSitemap();
$this->addProductsToSitemap();

// Generate
$this->sitemap->createSitemapIndex($domain, 'Today');
}

/**
* @throws \Magento\Framework\Exception\FileSystemException
*/
private function getPubPath() : string
{
return $this->directoryList->getPath('pub') . '/';
}

/**
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
private function getActiveProducts()
{
return $this->productCollection->get();
}

/**
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
private function getActiveCategories()
{
return $this->categoryCollection->get();
}

protected function addProductsToSitemap(): void
{
$activeProducts = $this->getActiveProducts();
if ($activeProducts->count() >= 1) {
/** @var ProductInterface $product */
foreach ($activeProducts->getItems() as $product) {
$productUrl = '/' . $product->getSku() . '/' . $product->getUrlKey();
$this->sitemap->addItem(
$productUrl,
1.0,
'daily',
$product->getUpdatedAt()
);
}
}
}

protected function addCategoriesToSitemap(): void
{
$activeCategories = $this->getActiveCategories();
if ($activeCategories->count() >= 1) {
/** @var CategoryInterface $category */
foreach ($activeCategories->getItems() as $category) {
$categoryUrl = '/' . $category->getUrlKey() . '-' . $category->getId();
$this->sitemap->addItem(
$categoryUrl,
1.0,
'daily',
$category->getUpdatedAt()
);
}
}
}

protected function addHomepageToSitemap() : void
{
$this->sitemap->addItem(
'/'
);
}
}
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 Vendic B.V.

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
38 changes: 38 additions & 0 deletions Model/CategoryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

/**
* @author tjitse (Vendic)
* Created on 26/03/2019 09:43
*/

namespace Vendic\VueStorefrontSitemap\Model;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

class CategoryCollection
{
/**
* @var CollectionFactory
*/
protected $collectionFactory;

public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}

/**
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function get()
{
$collection = $this->collectionFactory->create();
$collection->addFieldToSelect('url_key');
$collection->addFieldToSelect('level');
$collection->addFieldToFilter('url_key', ['neq' => null]);

return $collection;
}
}
42 changes: 42 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

/**
* @author tjitse (Vendic)
* Created on 23/03/2019 10:33
*/

namespace Vendic\VueStorefrontSitemap\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\LocalizedException;

class Configuration
{
const VUE_STOREFRONT_URL_CONFIG_PATH = 'vuestorefront/sitemap/vs_url';

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

public function getVueStorefrontUrl(): string
{
$url = $this->scopeConfig->getValue(
self::VUE_STOREFRONT_URL_CONFIG_PATH,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
if (!is_string($url)) {
throw new LocalizedException(
__('Invalid or no VueStorefront url entered for config path %1', self::VUE_STOREFRONT_URL_CONFIG_PATH)
);
}
return $url;
}
}
72 changes: 72 additions & 0 deletions Model/ProductCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);

/**
* @author tjitse (Vendic)
* Created on 22/03/2019 18:39
*/

namespace Vendic\VueStorefrontSitemap\Model;

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;

class ProductCollection
{

/**
* @var CollectionFactory
*/
protected $productCollectionFactory;
/**
* @var Status
*/
protected $productStatus;
/**
* @var Visibility
*/
protected $productVisibility;
/**
* @var StoreManagerInterface
*/
protected $storeManager;

public function __construct(
StoreManagerInterface $storeManager,
Status $productStatus,
CollectionFactory $productCollectionFactory,
Visibility $productVisibility
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;
$this->storeManager = $storeManager;
}

/**
* Get active products
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function get()
{
$collection = $this->productCollectionFactory->create();
// We need to add store filters first to avoid an Zend_Db_Statement_Exception https://github.com/magento/magento2/issues/15187
$collection->addStoreFilter($this->getDefaultStoreId());
$collection->addAttributeToSelect('url_key');
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

return $collection;
}

/**
* TODO make store ID configurable via Magento backend
* @return int
*/
private function getDefaultStoreId()
{
return $this->storeManager->getDefaultStoreView()->getId();
}
}
19 changes: 19 additions & 0 deletions Model/SitemapFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

/**
* @author tjitse (Vendic)
* Created on 22/03/2019 18:07
*/

namespace Vendic\VueStorefrontSitemap\Model;

use SitemapPHP\Sitemap;

class SitemapFactory
{
public function create(string $domain) : Sitemap
{
return new Sitemap($domain);
}
}
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# VueStorefront XML sitemap generator for Magento 2
**Note: this package isn't production ready. Please test thoroughly before using in production**

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:

**Categories:**

https://vuestorefronturl.com/urlkey-id

**Products:**

https://vuestorefronturl.com/sku/urlkey

## Installation
```
composer require composer require vendic/magento2-vuestorefront-xmlsitemap
```

Go to system configuration:
> Stores > Configuration > Vendic > VueStorefront
And set your Vuestorefront homepage url.

### Contributors
[Tjitse Efde](https://vendic.nl)

### License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
Loading

0 comments on commit 85f20f3

Please sign in to comment.