Skip to content

Commit 3409af0

Browse files
Barny ShergoldBarny Shergold
Barny Shergold
authored and
Barny Shergold
committed
Changed so that store view category is always used to create product URLs
1 parent 8513dfd commit 3409af0

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
174174
continue;
175175
}
176176

177-
// category should be loaded per appropriate store if category's URL key has been changed
178-
$categories[] = $this->getCategoryWithOverriddenUrlKey($storeId, $category);
177+
// Category should be loaded per appropriate store at all times. This is because whilst the URL key on the
178+
// category in focus might be unchanged, parent category URL keys might be
179+
$categories[] = $this->categoryRepository->get($category->getEntityId(), $storeId);
179180
}
180181

181182
$productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);
@@ -234,29 +235,6 @@ public function isCategoryProperForGenerating(Category $category, $storeId)
234235
return false;
235236
}
236237

237-
/**
238-
* Check if URL key has been changed
239-
*
240-
* Checks if URL key has been changed for provided category and returns reloaded category,
241-
* in other case - returns provided category.
242-
*
243-
* @param int $storeId
244-
* @param Category $category
245-
* @return Category
246-
*/
247-
private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
248-
{
249-
$isUrlKeyOverridden = $this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
250-
$storeId,
251-
$category->getEntityId(),
252-
Category::ENTITY
253-
);
254-
255-
if (!$isUrlKeyOverridden) {
256-
return $category;
257-
}
258-
return $this->categoryRepository->get($category->getEntityId(), $storeId);
259-
}
260238

261239
/**
262240
* Check config value of generate_category_product_rewrites

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogUrlRewrite\Test\Unit\Model;
99

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1011
use Magento\Catalog\Model\Category;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\CatalogUrlRewrite\Model\ObjectRegistry;
@@ -69,6 +70,9 @@ class ProductScopeRewriteGeneratorTest extends TestCase
6970
/** @var ScopeConfigInterface|MockObject */
7071
private $configMock;
7172

73+
/** @var CategoryRepositoryInterface|MockObject */
74+
private $categoryRepository;
75+
7276
protected function setUp(): void
7377
{
7478
$this->serializer = $this->createMock(Json::class);
@@ -126,6 +130,8 @@ function ($value) {
126130
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
127131
->getMock();
128132

133+
$this->categoryRepository = $this->getMockForAbstractClass(CategoryRepositoryInterface::class);
134+
129135
$this->productScopeGenerator = (new ObjectManager($this))->getObject(
130136
ProductScopeRewriteGenerator::class,
131137
[
@@ -137,7 +143,8 @@ function ($value) {
137143
'storeViewService' => $this->storeViewService,
138144
'storeManager' => $this->storeManager,
139145
'mergeDataProviderFactory' => $mergeDataProviderFactory,
140-
'config' => $this->configMock
146+
'config' => $this->configMock,
147+
'categoryRepository' => $this->categoryRepository
141148
]
142149
);
143150
$this->categoryMock = $this->getMockBuilder(Category::class)
@@ -215,6 +222,8 @@ public function testGenerationForSpecificStore()
215222
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
216223
->willReturn([]);
217224

225+
$this->categoryRepository->expects($this->once())->method('get')->willReturn($this->categoryMock);
226+
218227
$this->assertEquals(
219228
['category-1_1' => $canonical],
220229
$this->productScopeGenerator->generateForSpecificStoreView(1, [$this->categoryMock], $product, 1)

0 commit comments

Comments
 (0)