/
UrlRegeneration
UrlRegeneration
GitHub - magesuite/url-regeneration
This module adds an option for regenerating selected or all product URLs at once and for regenerating a given category URL or all category URLs at once.
- 1 Installation
- 2 Admin settings
- 3 Backend
- 3.1 Product Url Generator
- 3.1.1 vendor/creativestyle/magesuite-url-regeneration/Service/Product/UrlGenerator.php:46
- 3.1.2 vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php:128
- 3.1.3 vendor/magento/module-catalog-url-rewrite/Model/ProductScopeRewriteGenerator.php:187
- 3.1.4 vendor/magento/module-url-rewrite/Model/Storage/AbstractStorage.php:82
- 3.1.5 vendor/magento/module-catalog-url-rewrite/Model/Category/Plugin/Storage.php:59
- 3.2 Category Url Generator
- 3.2.1 vendor/creativestyle/magesuite-url-regeneration/Service/Category/UrlGenerator.php:39
- 3.2.2 vendor/magento/module-catalog-url-rewrite/Model/CategoryUrlRewriteGenerator.php:102
- 3.2.3 vendor/magento/module-catalog-url-rewrite/Model/CategoryUrlRewriteGenerator.php:102
- 3.2.4 vendor/magento/module-url-rewrite/Model/Storage/AbstractStorage.php:82
- 3.2.5 vendor/magento/module-catalog-url-rewrite/Model/Category/Plugin/Storage.php:59
- 3.1 Product Url Generator
- 4 Frontend
Installation
This module is part of MageSuite metapackage
Installation if metapackage is not used:
composer require "creativestyle/magesuite-url-regeneration" ^1.0.0
Admin settings
Product URLs can be regenerated from the product listing page in the admin panel:
Remember that all old URLs related to selected products (including 301 redirects) will be removed. This operation may take some time depending on amount of products selected.
Category URLs can be regenerated from any category edit page:
Backend
Backend documentation in progress
Product Url Generator
vendor/creativestyle/magesuite-url-regeneration/Service/Product/UrlGenerator.php:46
class UrlGenerator
{
public function regenerate($productIds = [])
{
$stores = $this->storeManager->getStores(false);
foreach ($stores as $store) {
$this->regenerateStoreUrls($store, $productIds);
}
}
protected function regenerateStoreUrls($store, $productIds = [])
{
.....
$products = $collection->load();
foreach ($products as $product) {
$product->setStoreId($storeId);
$this->urlPersist->deleteByData([
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_ID => $product->getId(),
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE,
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::STORE_ID => $storeId
]);
$newUrls = $this->productUrlRewriteGenerator->generate($product);
try {
$this->urlPersist->replace($newUrls);
} catch (\Exception $e) {} //phpcs:ignore
}
}
}
vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php:128
class ProductUrlRewriteGenerator
{
/** Generate product url rewrites */
public function generate(Product $product, $rootCategoryId = null)
{
.....
$urls = $this->isGlobalScope($storeId)
? $this->generateForGlobalScope($productCategories, $product, $rootCategoryId)
: $this->generateForSpecificStoreView($storeId, $productCategories, $product, $rootCategoryId);
return $urls;
}
protected function generateForSpecificStoreView(
.....
) {
return $this->getProductScopeRewriteGenerator()
->generateForSpecificStoreView($storeId, $productCategories, $product, $rootCategoryId);
}
}
vendor/magento/module-catalog-url-rewrite/Model/ProductScopeRewriteGenerator.php:187
/**
* Generates Product/Category URLs for different scopes
*/
class ProductScopeRewriteGenerator
{
/**
* Generate list of urls for specific store view
*/
public function generateForSpecificStoreView($storeId, $productCategories, Product $product, $rootCategoryId = null)
{
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
.....
return $mergeDataProvider->getData();
}
}
vendor/magento/module-url-rewrite/Model/Storage/AbstractStorage.php:82
abstract class AbstractStorage implements StorageInterface
{
public function replace(array $urls)
{
if (!$urls) {
return [];
}
return $this->doReplace($urls);
}
}
vendor/magento/module-catalog-url-rewrite/Model/Category/Plugin/Storage.php:59
class Storage
{
/**
* Save product/category urlRewrite association
*/
public function afterReplace(StorageInterface $object, array $result, array $urls)
{
$toSave = [];
foreach ($this->filterUrls($result) as $record) {
$metadata = $record->getMetadata();
$toSave[] = [
'url_rewrite_id' => $record->getUrlRewriteId(),
'category_id' => $metadata['category_id'],
'product_id' => $record->getEntityId(),
];
}
if (count($toSave) > 0) {
$this->productResource->saveMultiple($toSave);
}
return $result;
}
/**
* Filter urls
*/
protected function filterUrls(array $urls)
{
$filteredUrls = [];
.....
$existingUrls = $data ? $this->urlFinder->findAllByData($data) : [];
$mergeDataProviderForNewUrls = $this->mergeDataProviderFactory->create();
$mergeDataProviderForOldUrls = $this->mergeDataProviderFactory->create();
$mergeDataProviderForNewUrls->merge($filteredUrls);
$mergeDataProviderForOldUrls->merge($existingUrls);
return array_intersect_key(
$mergeDataProviderForOldUrls->getData(),
$mergeDataProviderForNewUrls->getData()
);
}
}
Category Url Generator
vendor/creativestyle/magesuite-url-regeneration/Service/Category/UrlGenerator.php:39
class UrlGenerator
{
public function regenerate($categoryId, $withSubcategories = false)
{
$stores = $this->storeManager->getStores(false);
foreach ($stores as $store) {
$this->deleteOldUrls($store, $categoryId, $withSubcategories);
}
foreach ($stores as $store) {
$this->regenerateStoreUrls($store, $categoryId, $withSubcategories);
}
}
protected function regenerateStoreUrls(\Magento\Store\Api\Data\StoreInterface $store, int $categoryId, bool $withSubcategories = false)
{
$category = $this->getCategoryByStore($categoryId, $store);
.....
$newUrls = $this->categoryUrlRewriteGenerator->generate($category);
try {
$this->urlPersist->replace($newUrls);
} catch (\Exception $e) {} //phpcs:ignore
.....
foreach ($category->getChildrenCategories() as $childCategory) {
$this->regenerateStoreUrls($store, $childCategory->getId(), true);
}
}
}
vendor/magento/module-catalog-url-rewrite/Model/CategoryUrlRewriteGenerator.php:102
class CategoryUrlRewriteGenerator
{
/**
* Generate list of urls.
*/
public function generate($category, $overrideStoreUrls = false, $rootCategoryId = null)
{
.....
$urls = $this->isGlobalScope($storeId)
? $this->generateForGlobalScope($category, $overrideStoreUrls, $rootCategoryId)
: $this->generateForSpecificStoreView($storeId, $category, $rootCategoryId);
return $urls;
}
}
vendor/magento/module-catalog-url-rewrite/Model/CategoryUrlRewriteGenerator.php:102
class CategoryUrlRewriteGenerator
{
/**
* Generate list of urls
*/
public function generate($category, $overrideStoreUrls = false, $rootCategoryId = null)
{
.....
$urls = $this->isGlobalScope($storeId)
? $this->generateForGlobalScope($category, $overrideStoreUrls, $rootCategoryId)
: $this->generateForSpecificStoreView($storeId, $category, $rootCategoryId);
return $urls;
}
/**
* Generate list of urls per store
*/
protected function generateForSpecificStoreView($storeId, Category $category = null, $rootCategoryId = null)
{
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
.....
return $mergeDataProvider->getData();
}
}
vendor/magento/module-url-rewrite/Model/Storage/AbstractStorage.php:82
abstract class AbstractStorage implements StorageInterface
{
public function replace(array $urls)
{
if (!$urls) {
return [];
}
return $this->doReplace($urls);
}
}
vendor/magento/module-catalog-url-rewrite/Model/Category/Plugin/Storage.php:59
class Storage
{
/**
* Save product/category urlRewrite association
*/
public function afterReplace(StorageInterface $object, array $result, array $urls)
{
$toSave = [];
foreach ($this->filterUrls($result) as $record) {
$metadata = $record->getMetadata();
$toSave[] = [
'url_rewrite_id' => $record->getUrlRewriteId(),
'category_id' => $metadata['category_id'],
'product_id' => $record->getEntityId(),
];
}
if (count($toSave) > 0) {
$this->productResource->saveMultiple($toSave);
}
return $result;
}
/**
* Filter urls
*/
protected function filterUrls(array $urls)
{
$filteredUrls = [];
.....
$existingUrls = $data ? $this->urlFinder->findAllByData($data) : [];
$mergeDataProviderForNewUrls = $this->mergeDataProviderFactory->create();
$mergeDataProviderForOldUrls = $this->mergeDataProviderFactory->create();
$mergeDataProviderForNewUrls->merge($filteredUrls);
$mergeDataProviderForOldUrls->merge($existingUrls);
return array_intersect_key(
$mergeDataProviderForOldUrls->getData(),
$mergeDataProviderForNewUrls->getData()
);
}
}
Frontend
There are no frontend functionalities in this module.
, multiple selections available,
Related content
SeoCanonical
SeoCanonical
Read with this
3.7.3. URL Regeneration
3.7.3. URL Regeneration
More like this
Gdpr
Read with this
UrlRewriteMassActions
UrlRewriteMassActions
More like this
Cache
Read with this
UrlRewrite
UrlRewrite
More like this