Skip to content

Commit 16dace3

Browse files
committed
Optimized code; modified logic of url rewrites regeneration; fixed issue of store filter in categories collection;
1 parent ce14739 commit 16dace3

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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.3.0] - 2018-10-09
7+
## [1.3.0] - 2018-10-29
88
### Added
99
- new option "--no-cache-clean"
1010
- new option "--no-cache-flush"
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1414
### Changed
1515
- optimized code
1616
- modified logic of url rewrites regeneration
17+
- fixed issue of store filter in categories collection
1718

1819
## [1.2.3] - 2018-10-03
1920
### Added

Console/Command/RegenerateUrlRewrites.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
104104
}
105105

106106
if ($this->_commandOptions['runCacheClean'] || $this->_commandOptions['runCacheFlush']) {
107-
$this->_output->writeln('Cache refreshing...');
107+
$this->_output->write('Cache refreshing...');
108108
if ($this->_commandOptions['runCacheClean']) {
109109
shell_exec('php bin/magento cache:clean');
110110
}
111111
if ($this->_commandOptions['runCacheFlush']) {
112112
shell_exec('php bin/magento cache:flush');
113113
}
114+
$this->_output->writeln(' Done');
114115
$this->_output->writeln('If you use some external cache mechanisms (e.g.: Redis, Varnish, etc.) - please, refresh this external cache.');
115116
}
116117

Console/Command/RegenerateUrlRewritesAbstract.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,24 @@ protected function _generateIdsRangeArray($idsRange, $type = 'product')
439439
return $result;
440440
}
441441

442+
/**
443+
* Get root category Id of specific store
444+
* @param string $storeId
445+
* @return integer
446+
*/
447+
protected function _getStoreRootCategoryId($storeId)
448+
{
449+
$result = 0;
450+
451+
// get existed Id's from this range in entity DB table
452+
$tableName = $this->_resource->getTableName('store_group');
453+
$sql = "SELECT root_category_id FROM {$tableName} WHERE default_store_id = {$storeId};";
454+
455+
$result = (int) $this->_resource->getConnection()->fetchOne($sql);
456+
457+
return $result;
458+
}
459+
442460
/**
443461
* @return Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator
444462
*/
@@ -511,12 +529,17 @@ protected function _getCategoriesCollection($storeId = 0, $categoriesFilter = []
511529
// get categories collection
512530
$categoriesCollection = $this->_categoryCollectionFactory->create()
513531
->addAttributeToSelect('*')
514-
->setStore($storeId)
515532
->addFieldToFilter('level', array('gt' => '1'))
516533
->setOrder('level', 'DESC')
517534
// use limit to avoid a "eating" of a memory
518535
->setPageSize($this->_collectionPageSize);
519536

537+
$rootCategoryId = $this->_getStoreRootCategoryId($storeId);
538+
if ($rootCategoryId > 0) {
539+
// we use this filter instead of "->setStore()" - because "setStore()" is not working now (another Magento issue)
540+
$categoriesCollection->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"));
541+
}
542+
520543
if (count($categoriesFilter) > 0) {
521544
$categoriesCollection->addAttributeToFilter('entity_id', array('in' => $categoriesFilter));
522545
}
@@ -628,7 +651,7 @@ protected function _categoryProcess($category, $storeId = 0)
628651
protected function _regenerateCategoryUrlRewrites($category, $storeId)
629652
{
630653
try {
631-
$categoryUrlRewriteResult = $this->_getCategoryUrlRewriteGenerator()->generate($category);
654+
$categoryUrlRewriteResult = $this->_getCategoryUrlRewriteGenerator()->generate($category, true);
632655
$this->_doBunchReplaceUrlRewrites($categoryUrlRewriteResult);
633656

634657
$productUrlRewriteResult = $this->_getUrlRewriteHandler()->generateProductUrlRewrites($category);

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Extension homepage: https://github.com/olegkoval/magento2-regenerate_url_rewrite
66
## DONATIONS / SUPPORT ME ON:
77
* [Patreon](https://www.patreon.com/olegkoval)
88
* [Fondy](https://api.fondy.eu/s/ghYyR)
9-
* [Liqpay](https://www.liqpay.ua/en/checkout/card/380983346262)
109

1110
## INSTALLATION
1211

@@ -51,9 +50,11 @@ or
5150
* do not run cache:flush at the end of Url rewrites generation:
5251
>`$> bin/magento ok:urlrewrites:regenerate --no-cache-flush`
5352
54-
* do not clean current products url_key values (take into account that new URL's will be generated based on the current url_key values, not products "name" values, \*\* see bottom):
53+
* do not clean current products url_key values (take into account that new URL's will be generated based on the current url_key values, not products "name" values) \*\*:
5554
>`$> bin/magento ok:urlrewrites:regenerate --no-clean-url-key`
5655
56+
\*\* When Magento generate URL rewrites for some product it check if product have a url_key value and use it (and do not take into acount "name" value, even if you modified it), otherwise (if url_key not exists or have an empty value) Magento use a product name to generate new url_key and then use it to generate a URL rewrites.
57+
5758
* do not display a progress dots in the console (usefull for a stores with a big number of products):
5859
>`$> bin/magento ok:urlrewrites:regenerate --no-progress`
5960
@@ -79,5 +80,3 @@ Oleg Koval
7980

8081
-------------
8182
\* see: http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands.html
82-
83-
\*\* when Magento generate URL rewrites for some product it check if product have a url_key value and use it (and do not take into acount "name" value, even if you modified it), otherwise (if url_key not exists or have an empty value) Magento use a product name to generate new url_key and then use it to generate a URL rewrites.

0 commit comments

Comments
 (0)