Skip to content

Commit f94679c

Browse files
ENGCOM-5330: #23074: update correct product URL rewrites after changing category url key #23309
- Merge Pull Request #23309 from sta1r/magento2:23074-update-url-key - Merged commits: 1. 8fe7c88 2. 60b577e 3. 32de280 4. 0da86ab 5. 2d0fb70
2 parents bcfe16f + 2d0fb70 commit f94679c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,19 @@ public function generateProductUrlRewrites(Category $category): array
143143
if ($category->getChangedProductIds()) {
144144
$this->generateChangedProductUrls($mergeDataProvider, $category, $storeId, $saveRewriteHistory);
145145
} else {
146-
$mergeDataProvider->merge(
147-
$this->getCategoryProductsUrlRewrites(
148-
$category,
149-
$storeId,
150-
$saveRewriteHistory,
151-
$category->getEntityId()
152-
)
153-
);
146+
$categoryStoreIds = $this->getCategoryStoreIds($category);
147+
148+
foreach ($categoryStoreIds as $categoryStoreId) {
149+
$this->isSkippedProduct[$category->getEntityId()] = [];
150+
$mergeDataProvider->merge(
151+
$this->getCategoryProductsUrlRewrites(
152+
$category,
153+
$categoryStoreId,
154+
$saveRewriteHistory,
155+
$category->getEntityId()
156+
)
157+
);
158+
}
154159
}
155160

156161
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
@@ -241,7 +246,7 @@ private function getCategoryProductsUrlRewrites(
241246
$productCollection = $this->productCollectionFactory->create();
242247

243248
$productCollection->addCategoriesFilter(['eq' => [$category->getEntityId()]])
244-
->setStoreId($storeId)
249+
->addStoreFilter($storeId)
245250
->addAttributeToSelect('name')
246251
->addAttributeToSelect('visibility')
247252
->addAttributeToSelect('url_key')

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/UrlRewriteHandlerTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ public function testGenerateProductUrlRewrites()
138138
->willReturn(1);
139139
$category->expects($this->any())
140140
->method('getData')
141-
->with('save_rewrites_history')
142-
->willReturn(true);
141+
->withConsecutive(
142+
[$this->equalTo('save_rewrites_history')],
143+
[$this->equalTo('initial_setup_flag')]
144+
)
145+
->willReturnOnConsecutiveCalls(
146+
true,
147+
null
148+
);
143149

144150
/* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $childCategory1 */
145151
$childCategory1 = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
@@ -175,6 +181,7 @@ public function testGenerateProductUrlRewrites()
175181
->method('addIdFilter')
176182
->willReturnSelf();
177183
$productCollection->expects($this->any())->method('setStoreId')->willReturnSelf();
184+
$productCollection->expects($this->any())->method('addStoreFilter')->willReturnSelf();
178185
$productCollection->expects($this->any())->method('addAttributeToSelect')->willReturnSelf();
179186
$iterator = new \ArrayIterator([]);
180187
$productCollection->expects($this->any())->method('getIterator')->will($this->returnValue($iterator));

0 commit comments

Comments
 (0)