Skip to content

Commit 44cca2e

Browse files
committed
Updated Url Rewrite preparing function. Updated logic of Url Rewrite regeneration via category entity. Updated save logic.
1 parent 01701fe commit 44cca2e

5 files changed

+24
-14
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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.3] - 2020-03-21
8+
### Changed
9+
- updated Url Rewrite preparing function
10+
- updated logic of Url Rewrite regeneration via category entity
11+
- updated save logic
12+
713
## [1.5.2] - 2020-03-18
814
### Changed
915
- updated logic of Url Rewrite regeneration via category entity

Model/AbstractRegenerateRewrites.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public function saveUrlRewrites($urlRewrites, $entityData = [])
128128
try {
129129
$this->_getResourceConnection()->getConnection()->insertOnDuplicate(
130130
$this->_getMainTableName(),
131-
$data
131+
$data,
132+
['request_path', 'metadata']
132133
);
133134
$this->_getResourceConnection()->getConnection()->commit();
134135

@@ -288,7 +289,8 @@ protected function _updateSecondaryTable()
288289
try {
289290
$this->_getResourceConnection()->getConnection()->insertOnDuplicate(
290291
$this->_getSecondaryTableName(),
291-
$data
292+
$data,
293+
['product_id']
292294
);
293295
$this->_getResourceConnection()->getConnection()->commit();
294296

@@ -312,12 +314,17 @@ protected function _prepareUrlRewrites($urlRewrites)
312314
$rewrite = $urlRewrite->toArray();
313315

314316
// check if same Url Rewrite already exists
315-
$originalRequestPath = $rewrite['request_path'];
317+
$originalRequestPath = trim($rewrite['request_path']);
318+
319+
// skip empty Url Rewrites - I don't know how this possible, but it happens in Magento:
320+
// maybe someone did import product programmatically and product(s) name(s) are empty
321+
if (empty($originalRequestPath)) continue;
322+
316323
$index = 0;
317324
while ($this->_urlRewriteExists($rewrite)) {
318325
$index++;
319326
$pathParts = pathinfo($originalRequestPath);
320-
$rewrite['request_path'] = $pathParts['dirname'] . $pathParts['filename'] . '-'. $index . (!empty($pathParts['extension']) ? $pathParts['extension'] : '');
327+
$rewrite['request_path'] = trim($pathParts['dirname'], '.') . $pathParts['filename'] . '-'. $index . (!empty($pathParts['extension']) ? '.'. $pathParts['extension'] : '');
321328
}
322329

323330
$result[] = $rewrite;

Model/RegenerateCategoryRewrites.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,11 @@ protected function categoryProcess($category, $storeId = 0)
223223
$this->saveUrlRewrites($categoryUrlRewriteResult);
224224
}
225225

226-
// if config option "Use Categories Path for Product URLs" is "Yes" then regenerate product urls
227-
if ($this->helper->useCategoriesPathForProductUrls($storeId)) {
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);
233-
}
226+
$productsIds = $this->_getCategoriesProductsIds($category->getAllChildren());
227+
if (!empty($productsIds)) {
228+
$this->regenerateProductRewrites->regenerateOptions = $this->regenerateOptions;
229+
$this->regenerateProductRewrites->regenerateOptions['showProgress'] = false;
230+
$this->regenerateProductRewrites->regenerateProductsRangeUrlRewrites($productsIds, $storeId);
234231
}
235232

236233
//frees memory for maps that are self-initialized in multiple classes that were called by the generators

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"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.2",
6+
"version": "1.5.3",
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.2">
13+
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.5.3">
1414
<sequence>
1515
<module name="Magento_Store"/>
1616
<module name="Magento_Catalog"/>

0 commit comments

Comments
 (0)