Skip to content

Commit 01701fe

Browse files
committed
Updated logic of Url Rewrite regeneration via category entity. CLI options logic optimized (for category entity).
1 parent 3a06060 commit 01701fe

8 files changed

+86
-13
lines changed

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.5.2] - 2020-03-18
8+
### Changed
9+
- updated logic of Url Rewrite regeneration via category entity
10+
- CLI options logic optimized (for category entity)
11+
12+
## [1.5.1] - 2020-03-08
13+
### Changed
14+
- fixed issue of url_key and url rewrites regeneration based on product name value
15+
16+
## [1.5.0] - 2020-02-26
17+
### Changed
18+
- revised and restructured code
19+
- modified functional-logic of extension
20+
- removed option "--check-use-category-in-product-url"
21+
722
## [1.4.3] - 2019-05-12
823
### Added
924
- new option "no-regen-url-key"

Console/Command/RegenerateUrlRewrites.php

+6
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public function getCommandOptions()
236236
'category'
237237
);
238238
$distinctOptionsUsed++;
239+
240+
// if this option was used then for 100% user want to regenerate entity type "category"
241+
$this->_commandOptions['entityType'] = self::INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY;
239242
}
240243

241244
if (isset($options[self::INPUT_KEY_CATEGORY_ID])) {
@@ -246,6 +249,9 @@ public function getCommandOptions()
246249
} else {
247250
$distinctOptionsUsed++;
248251
}
252+
253+
// if this option was used then for 100% user want to regenerate entity type "category"
254+
$this->_commandOptions['entityType'] = self::INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY;
249255
}
250256

251257
if (

Helper/Regenerate.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public function getSupportMeText()
5050
{
5151
return [
5252
'Please, support me on:',
53-
'https://www.patreon.com/olegkoval'
53+
'https://www.patreon.com/olegkoval',
54+
'You can hire me via Upwork:',
55+
'https://www.upwork.com/o/profiles/users/~01a40656da65c38d3a/'
5456
];
5557
}
5658

Model/AbstractRegenerateRewrites.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ abstract class AbstractRegenerateRewrites
4747
*/
4848
protected $secondaryDbTable;
4949

50+
/**
51+
* @var string
52+
*/
53+
protected $categoryProductsDbTable;
54+
5055
/**
5156
* Regenerate Rewrites custom options
5257
* @var array
@@ -137,7 +142,7 @@ public function saveUrlRewrites($urlRewrites, $entityData = [])
137142

138143
/**
139144
* Show a progress bar in the console
140-
* @return void
145+
* @param int $size
141146
*/
142147
protected function _showProgress($size = 70)
143148
{
@@ -199,6 +204,18 @@ protected function _getSecondaryTableName()
199204
return $this->secondaryDbTable;
200205
}
201206

207+
/**
208+
* @return string
209+
*/
210+
protected function _getCategoryProductsTableName()
211+
{
212+
if (empty($this->categoryProductsDbTable)) {
213+
$this->categoryProductsDbTable = $this->_getResourceConnection()->getTableName('catalog_category_product');
214+
}
215+
216+
return $this->categoryProductsDbTable;
217+
}
218+
202219
/**
203220
* Delete current Url Rewrites
204221
* @param array $entitiesData

Model/RegenerateCategoryRewrites.php

+38-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGeneratorFactory;
2020
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory;
2121
use Magento\CatalogUrlRewrite\Observer\UrlRewriteHandlerFactory;
22+
use OlegKoval\RegenerateUrlRewrites\Model\RegenerateProductRewrites;
2223

2324
class RegenerateCategoryRewrites extends AbstractRegenerateRewrites
2425
{
@@ -77,6 +78,11 @@ class RegenerateCategoryRewrites extends AbstractRegenerateRewrites
7778
*/
7879
protected $urlRewriteHandler;
7980

81+
/**
82+
* @var RegenerateProductRewrites
83+
*/
84+
protected $regenerateProductRewrites;
85+
8086
/**
8187
* RegenerateCategoryRewrites constructor.
8288
* @param RegenerateHelper $helper
@@ -89,7 +95,8 @@ public function __construct(
8995
DatabaseMapPool\Proxy $databaseMapPool,
9096
CategoryUrlPathGeneratorFactory\Proxy $categoryUrlPathGeneratorFactory,
9197
CategoryUrlRewriteGeneratorFactory\Proxy $categoryUrlRewriteGeneratorFactory,
92-
UrlRewriteHandlerFactory\Proxy $urlRewriteHandlerFactory
98+
UrlRewriteHandlerFactory\Proxy $urlRewriteHandlerFactory,
99+
RegenerateProductRewrites $regenerateProductRewrites
93100
)
94101
{
95102
parent::__construct($helper, $resourceConnection);
@@ -99,6 +106,7 @@ public function __construct(
99106
$this->categoryUrlPathGeneratorFactory = $categoryUrlPathGeneratorFactory;
100107
$this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory;
101108
$this->urlRewriteHandlerFactory = $urlRewriteHandlerFactory;
109+
$this->regenerateProductRewrites = $regenerateProductRewrites;
102110

103111
$this->dataUrlRewriteClassNames = [
104112
DataCategoryUrlRewriteDatabaseMap::class,
@@ -202,7 +210,7 @@ protected function categoryProcess($category, $storeId = 0)
202210

203211
if (!$this->regenerateOptions['noRegenUrlKey']) {
204212
$category->setOrigData('url_key', null);
205-
$category->setUrlKey($this->_getCategoryUrlPathGenerator()->getUrlKey($category));
213+
$category->setUrlKey($this->_getCategoryUrlPathGenerator()->getUrlKey($category->setUrlKey(null)));
206214
$category->getResource()->saveAttribute($category, 'url_key');
207215
}
208216

@@ -217,10 +225,11 @@ protected function categoryProcess($category, $storeId = 0)
217225

218226
// if config option "Use Categories Path for Product URLs" is "Yes" then regenerate product urls
219227
if ($this->helper->useCategoriesPathForProductUrls($storeId)) {
220-
$productUrlRewriteResult = $this->_getUrlRewriteHandler()->generateProductUrlRewrites($category);
221-
if (!empty($productUrlRewriteResult)) {
222-
$productUrlRewriteResult = $this->helper->sanitizeProductUrlRewrites($productUrlRewriteResult);
223-
$this->saveUrlRewrites($productUrlRewriteResult);
228+
$productsIds = $this->_getCategoriesProductsIds($category->getAllChildren());
229+
if (!empty($productsIds)) {
230+
$this->regenerateProductRewrites->regenerateOptions = $this->regenerateOptions;
231+
$this->regenerateProductRewrites->regenerateOptions['showProgress'] = false;
232+
$this->regenerateProductRewrites->regenerateProductsRangeUrlRewrites($productsIds, $storeId);
224233
}
225234
}
226235

@@ -263,6 +272,29 @@ protected function _getCategoriesCollection($categoriesFilter = [], $storeId = 0
263272
return $categoriesCollection;
264273
}
265274

275+
/**
276+
* Get products Ids which are related to specific categories
277+
* @param string $categoryIds
278+
* @return array
279+
*/
280+
protected function _getCategoriesProductsIds($categoryIds = '')
281+
{
282+
$result = [];
283+
284+
if (!empty($categoryIds)) {
285+
$select = $this->_getResourceConnection()->getConnection()->select()
286+
->from($this->_getCategoryProductsTableName(), ['product_id'])
287+
->where("category_id IN ({$categoryIds})");
288+
$rows = $this->_getResourceConnection()->getConnection()->fetchAll($select);
289+
290+
foreach ($rows as $row) {
291+
$result[] = $row['product_id'];
292+
}
293+
}
294+
295+
return $result;
296+
}
297+
266298
/**
267299
* Get category Url Path generator
268300
* @return mixed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Extension homepage: https://github.com/olegkoval/magento2-regenerate_url_rewrite
99

1010
## DONATIONS / SUPPORT ME ON
1111
* [Patreon](https://www.patreon.com/olegkoval)
12+
* You can hire me via [Upwork](https://www.upwork.com/o/profiles/users/~01a40656da65c38d3a/)
1213

1314
## INSTALLATION
1415

@@ -76,8 +77,6 @@ or
7677
>`$> php bin/magento ok:urlrewrites:regenerate --no-progress`
7778
7879
#### REGENERATE URL REWRITES OF CATEGORY
79-
WARNING: When you regenerate Url Rewrites of some category then you also regenerate Url Rewrites of all products from this category AND Url Rewrites of all sub-categories (all levels - Magento use recursive logic) AND all products from this sub-categories. This is the built-in Magento logic. So, take into account that regenerating of Url Rewrites of any category (specially from top level) is a "heavy" process and it's take a time.
80-
8180
* to regenerate Url Rewrites of all categories in all stores set entity type to "category":
8281
>`$> php bin/magento ok:urlrewrites:regenerate --entity-type=category`
8382
@@ -89,6 +88,8 @@ WARNING: When you regenerate Url Rewrites of some category then you also regener
8988
9089
\* if in the range you have a gap of ID's (in range 4-12 category with ID "6" not exists) - do not worry, script handle this.
9190

91+
\*\* If you use options `--category-id` or `--categories-range` then you can skip option `--entity-type=category` - extension will understand that you want to use a category entity.
92+
9293
### YOU CAN COMBINE OPTIONS
9394
>`$> php bin/magento ok:urlrewrites:regenerate --store-id=2 --save-old-urls --no-regen-url-key --no-reindex`
9495

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "olegkoval/magento2-regenerate-url-rewrites",
3-
"description": "Add into Magento 2 a CLI feature which allow to regenerate a Url rewrites of products and categories",
3+
"description": "Add into Magento 2 a CLI feature which allow to regenerate a Url Rewrites of products and categories",
44
"keywords": ["magento", "magento2 extension", "magento 2 extension", "extension", "module", "magento2 module", "magento 2 module"],
55
"type": "magento2-module",
6-
"version": "1.5.1",
6+
"version": "1.5.2",
77
"homepage": "https://github.com/olegkoval/magento2-regenerate_url_rewrites",
88
"authors": [
99
{

etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
-->
1212
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
13-
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.5.1">
13+
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.5.2">
1414
<sequence>
1515
<module name="Magento_Store"/>
1616
<module name="Magento_Catalog"/>

0 commit comments

Comments
 (0)