Skip to content

Commit

Permalink
Fixed issue of non-empty/non-false "request_path" of product entity. …
Browse files Browse the repository at this point in the history
…Modified logic of Url Rewrite db table updates.
  • Loading branch information
olegkoval committed Mar 21, 2020
1 parent 44cca2e commit fb037a1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file.

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

## [1.5.3] - 2020-03-21
## [1.5.4] - 2020-03-21
### Changed
- fixed issue of non-empty/non-false "request_path" of product entity.
- modified logic of Url Rewrite db table updates

## [1.5.3] - 2020-03-20
### Changed
- updated Url Rewrite preparing function
- updated logic of Url Rewrite regeneration via category entity
Expand Down
29 changes: 17 additions & 12 deletions Model/AbstractRegenerateRewrites.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,23 @@ protected function _updateSecondaryTable()
$data = $this->_getResourceConnection()->getConnection()->fetchAll($select);

if (!empty($data)) {
$this->_getResourceConnection()->getConnection()->beginTransaction();
try {
$this->_getResourceConnection()->getConnection()->insertOnDuplicate(
$this->_getSecondaryTableName(),
$data,
['product_id']
);
$this->_getResourceConnection()->getConnection()->commit();

} catch (\Exception $e) {
$this->_getResourceConnection()->getConnection()->rollBack();
throw $e;
// I'm using row-by-row inserts because some products/categories not exists in entity tables but Url Rewrites
// for this entities still exists in url_rewrite DB table.
// This is the issue of Magento EE (Data integrity/assurance of the accuracy and consistency of data)
// and this extension was made to not fix this, I just avoid this issue
foreach ($data as $row) {
$this->_getResourceConnection()->getConnection()->beginTransaction();
try {
$this->_getResourceConnection()->getConnection()->insertOnDuplicate(
$this->_getSecondaryTableName(),
$row,
['product_id']
);
$this->_getResourceConnection()->getConnection()->commit();

} catch (\Exception $e) {
$this->_getResourceConnection()->getConnection()->rollBack();
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions Model/RegenerateProductRewrites.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,21 @@ public function processProduct($entity, $storeId = 0)
$entity->setData('save_rewrites_history', true);
}

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

$this->_getProductAction()->updateAttributes(
[$entity->getId()],
$updateAttributes,
$storeId
);

$urlRewrites = $this->_getProductUrlRewriteGenerator()->generate($entity);
$urlRewrites = $this->helper->sanitizeProductUrlRewrites($urlRewrites);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Add into Magento 2 a CLI feature which allow to regenerate a Url Rewrites of products and categories",
"keywords": ["magento", "magento2 extension", "magento 2 extension", "extension", "module", "magento2 module", "magento 2 module"],
"type": "magento2-module",
"version": "1.5.3",
"version": "1.5.4",
"homepage": "https://github.com/olegkoval/magento2-regenerate_url_rewrites",
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.5.3">
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.5.4">
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Catalog"/>
Expand Down

0 comments on commit fb037a1

Please sign in to comment.