Skip to content

Commit fb037a1

Browse files
committed
Fixed issue of non-empty/non-false "request_path" of product entity. Modified logic of Url Rewrite db table updates.
1 parent 44cca2e commit fb037a1

5 files changed

+36
-20
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +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
7+
## [1.5.4] - 2020-03-21
8+
### Changed
9+
- fixed issue of non-empty/non-false "request_path" of product entity.
10+
- modified logic of Url Rewrite db table updates
11+
12+
## [1.5.3] - 2020-03-20
813
### Changed
914
- updated Url Rewrite preparing function
1015
- updated logic of Url Rewrite regeneration via category entity

Model/AbstractRegenerateRewrites.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,23 @@ protected function _updateSecondaryTable()
285285
$data = $this->_getResourceConnection()->getConnection()->fetchAll($select);
286286

287287
if (!empty($data)) {
288-
$this->_getResourceConnection()->getConnection()->beginTransaction();
289-
try {
290-
$this->_getResourceConnection()->getConnection()->insertOnDuplicate(
291-
$this->_getSecondaryTableName(),
292-
$data,
293-
['product_id']
294-
);
295-
$this->_getResourceConnection()->getConnection()->commit();
296-
297-
} catch (\Exception $e) {
298-
$this->_getResourceConnection()->getConnection()->rollBack();
299-
throw $e;
288+
// I'm using row-by-row inserts because some products/categories not exists in entity tables but Url Rewrites
289+
// for this entities still exists in url_rewrite DB table.
290+
// This is the issue of Magento EE (Data integrity/assurance of the accuracy and consistency of data)
291+
// and this extension was made to not fix this, I just avoid this issue
292+
foreach ($data as $row) {
293+
$this->_getResourceConnection()->getConnection()->beginTransaction();
294+
try {
295+
$this->_getResourceConnection()->getConnection()->insertOnDuplicate(
296+
$this->_getSecondaryTableName(),
297+
$row,
298+
['product_id']
299+
);
300+
$this->_getResourceConnection()->getConnection()->commit();
301+
302+
} catch (\Exception $e) {
303+
$this->_getResourceConnection()->getConnection()->rollBack();
304+
}
300305
}
301306
}
302307

Model/RegenerateProductRewrites.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,21 @@ public function processProduct($entity, $storeId = 0)
183183
$entity->setData('save_rewrites_history', true);
184184
}
185185

186+
// reset url_path to null, we need this to set a flag to use a Url Rewrites:
187+
// see logic in core Product Url model: \Magento\Catalog\Model\Product\Url::getUrl()
188+
// if "request_path" is not null or equal to "false" then Magento do not serach and do not use Url Rewrites
189+
$updateAttributes = ['url_path' => null];
186190
if (!$this->regenerateOptions['noRegenUrlKey']) {
187191
$generatedKey = $this->_getProductUrlPathGenerator()->getUrlKey($entity->setUrlKey(null));
188-
$this->_getProductAction()->updateAttributes(
189-
[$entity->getId()],
190-
['url_path' => null, 'url_key' => $generatedKey],
191-
$storeId
192-
);
192+
$updateAttributes['url_key'] = $generatedKey;
193193
}
194194

195+
$this->_getProductAction()->updateAttributes(
196+
[$entity->getId()],
197+
$updateAttributes,
198+
$storeId
199+
);
200+
195201
$urlRewrites = $this->_getProductUrlRewriteGenerator()->generate($entity);
196202
$urlRewrites = $this->helper->sanitizeProductUrlRewrites($urlRewrites);
197203

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.3",
6+
"version": "1.5.4",
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.3">
13+
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.5.4">
1414
<sequence>
1515
<module name="Magento_Store"/>
1616
<module name="Magento_Catalog"/>

0 commit comments

Comments
 (0)