Skip to content

Commit f0f7c39

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-91650' into EPAM-PR-30
2 parents e1821e5 + 7779c37 commit f0f7c39

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
100100
}
101101

102102
$mapsGenerated = false;
103-
if ($category->dataHasChangedFor('url_key')
104-
|| $category->dataHasChangedFor('is_anchor')
105-
|| !empty($category->getChangedProductIds())
106-
) {
103+
if ($this->isCategoryHasChanged($category)) {
107104
if ($category->dataHasChangedFor('url_key')) {
108105
$categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category);
109106
$this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult);
110107
}
111-
$productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category);
112-
$this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult);
108+
if ($this->isChangedOnlyProduct($category)) {
109+
$productUrlRewriteResult =
110+
$this->urlRewriteHandler->updateProductUrlRewritesForChangedProduct($category);
111+
$this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult);
112+
} else {
113+
$productUrlRewriteResult = $this->urlRewriteHandler->generateProductUrlRewrites($category);
114+
$this->urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult);
115+
}
113116
$mapsGenerated = true;
114117
}
115118

@@ -119,6 +122,38 @@ public function execute(\Magento\Framework\Event\Observer $observer)
119122
}
120123
}
121124

125+
/**
126+
* Check is category changed changed.
127+
*
128+
* @param Category $category
129+
* @return bool
130+
*/
131+
private function isCategoryHasChanged(Category $category): bool
132+
{
133+
if ($category->dataHasChangedFor('url_key')
134+
|| $category->dataHasChangedFor('is_anchor')
135+
|| !empty($category->getChangedProductIds())) {
136+
return true;
137+
}
138+
return false;
139+
}
140+
141+
/**
142+
* Check is only product changed.
143+
*
144+
* @param Category $category
145+
* @return bool
146+
*/
147+
private function isChangedOnlyProduct(Category $category): bool
148+
{
149+
if (!empty($category->getChangedProductIds())
150+
&& !$category->dataHasChangedFor('is_anchor')
151+
&& !$category->dataHasChangedFor('url_key')) {
152+
return true;
153+
}
154+
return false;
155+
}
156+
122157
/**
123158
* In case store_id is not set for category then we can assume that it was passed through product import.
124159
* Store group must have only one root category, so receiving category's path and checking if one of it parts

app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
2525

2626
/**
27+
* Class for management url rewrites.
28+
*
2729
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2830
*/
2931
class UrlRewriteHandler
@@ -156,6 +158,30 @@ public function generateProductUrlRewrites(Category $category): array
156158
}
157159

158160
/**
161+
* Update product url rewrites for changed product.
162+
*
163+
* @param Category $category
164+
* @return array
165+
*/
166+
public function updateProductUrlRewritesForChangedProduct(Category $category): array
167+
{
168+
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
169+
$this->isSkippedProduct[$category->getEntityId()] = [];
170+
$saveRewriteHistory = (bool)$category->getData('save_rewrites_history');
171+
$storeIds = $this->getCategoryStoreIds($category);
172+
173+
if ($category->getChangedProductIds()) {
174+
foreach ($storeIds as $storeId) {
175+
$this->generateChangedProductUrls($mergeDataProvider, $category, (int)$storeId, $saveRewriteHistory);
176+
}
177+
}
178+
179+
return $mergeDataProvider->getData();
180+
}
181+
182+
/**
183+
* Delete category rewrites for children.
184+
*
159185
* @param Category $category
160186
* @return void
161187
*/
@@ -184,6 +210,8 @@ public function deleteCategoryRewritesForChildren(Category $category)
184210
}
185211

186212
/**
213+
* Get category products url rewrites.
214+
*
187215
* @param Category $category
188216
* @param int $storeId
189217
* @param bool $saveRewriteHistory

app/code/Magento/ProductAlert/Model/ResourceModel/AbstractResource.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\ProductAlert\Model\ResourceModel;
77

8+
use Magento\Framework\Model\AbstractModel;
9+
810
/**
911
* Product alert for back in abstract resource model
1012
*
@@ -15,13 +17,13 @@ abstract class AbstractResource extends \Magento\Framework\Model\ResourceModel\D
1517
/**
1618
* Retrieve alert row by object parameters
1719
*
18-
* @param \Magento\Framework\Model\AbstractModel $object
20+
* @param AbstractModel $object
1921
* @return array|false
2022
*/
21-
protected function _getAlertRow(\Magento\Framework\Model\AbstractModel $object)
23+
protected function _getAlertRow(AbstractModel $object)
2224
{
2325
$connection = $this->getConnection();
24-
if ($object->getCustomerId() && $object->getProductId() && $object->getWebsiteId()) {
26+
if ($this->isExistAllBindIds($object)) {
2527
$select = $connection->select()->from(
2628
$this->getMainTable()
2729
)->where(
@@ -30,24 +32,41 @@ protected function _getAlertRow(\Magento\Framework\Model\AbstractModel $object)
3032
'product_id = :product_id'
3133
)->where(
3234
'website_id = :website_id'
35+
)->where(
36+
'store_id = :store_id'
3337
);
3438
$bind = [
3539
':customer_id' => $object->getCustomerId(),
3640
':product_id' => $object->getProductId(),
3741
':website_id' => $object->getWebsiteId(),
42+
':store_id' => $object->getStoreId()
3843
];
3944
return $connection->fetchRow($select, $bind);
4045
}
4146
return false;
4247
}
4348

49+
/**
50+
* Is exists all bind ids.
51+
*
52+
* @param AbstractModel $object
53+
* @return bool
54+
*/
55+
private function isExistAllBindIds(AbstractModel $object): bool
56+
{
57+
return ($object->getCustomerId()
58+
&& $object->getProductId()
59+
&& $object->getWebsiteId()
60+
&& $object->getStoreId());
61+
}
62+
4463
/**
4564
* Load object data by parameters
4665
*
47-
* @param \Magento\Framework\Model\AbstractModel $object
66+
* @param AbstractModel $object
4867
* @return $this
4968
*/
50-
public function loadByParam(\Magento\Framework\Model\AbstractModel $object)
69+
public function loadByParam(AbstractModel $object)
5170
{
5271
$row = $this->_getAlertRow($object);
5372
if ($row) {
@@ -59,13 +78,13 @@ public function loadByParam(\Magento\Framework\Model\AbstractModel $object)
5978
/**
6079
* Delete all customer alerts on website
6180
*
62-
* @param \Magento\Framework\Model\AbstractModel $object
81+
* @param AbstractModel $object
6382
* @param int $customerId
6483
* @param int $websiteId
6584
* @return $this
6685
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6786
*/
68-
public function deleteCustomer(\Magento\Framework\Model\AbstractModel $object, $customerId, $websiteId = null)
87+
public function deleteCustomer(AbstractModel $object, $customerId, $websiteId = null)
6988
{
7089
$connection = $this->getConnection();
7190
$where = [];

0 commit comments

Comments
 (0)