Skip to content

Commit e46f719

Browse files
author
Anna Bukatar
committed
ACP2E-1304: Category Product URL is not redirecting to SEO friendly url / SEO friendly url does not exist
1 parent 5a022f6 commit e46f719

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
99
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Catalog\Model\Category;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator;
@@ -78,6 +79,11 @@ class ProductScopeRewriteGenerator
7879
*/
7980
private $categoryRepository;
8081

82+
/**
83+
* @var ProductRepositoryInterface
84+
*/
85+
private $productRepository;
86+
8187
/**
8288
* @param StoreViewService $storeViewService
8389
* @param StoreManagerInterface $storeManager
@@ -89,6 +95,7 @@ class ProductScopeRewriteGenerator
8995
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
9096
* @param CategoryRepositoryInterface|null $categoryRepository
9197
* @param ScopeConfigInterface|null $config
98+
* @param ProductRepositoryInterface|null $productRepository
9299
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
93100
*/
94101
public function __construct(
@@ -101,7 +108,8 @@ public function __construct(
101108
AnchorUrlRewriteGenerator $anchorUrlRewriteGenerator,
102109
MergeDataProviderFactory $mergeDataProviderFactory = null,
103110
CategoryRepositoryInterface $categoryRepository = null,
104-
ScopeConfigInterface $config = null
111+
ScopeConfigInterface $config = null,
112+
ProductRepositoryInterface $productRepository = null
105113
) {
106114
$this->storeViewService = $storeViewService;
107115
$this->storeManager = $storeManager;
@@ -117,6 +125,8 @@ public function __construct(
117125
$this->categoryRepository = $categoryRepository ?:
118126
ObjectManager::getInstance()->get(CategoryRepositoryInterface::class);
119127
$this->config = $config ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
128+
$this->productRepository = $productRepository ?:
129+
ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
120130
}
121131

122132
/**
@@ -144,15 +154,21 @@ public function generateForGlobalScope($productCategories, Product $product, $ro
144154
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
145155

146156
foreach ($product->getStoreIds() as $id) {
147-
if (!$this->isGlobalScope($id) &&
148-
!$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
157+
if (!$this->isGlobalScope($id)) {
158+
if (!$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
149159
$id,
150160
$productId,
151161
Product::ENTITY
152162
)) {
153-
$mergeDataProvider->merge(
154-
$this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId)
155-
);
163+
$mergeDataProvider->merge(
164+
$this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId)
165+
);
166+
} else {
167+
$scopedProduct = $this->productRepository->getById($productId, false, $id);
168+
$mergeDataProvider->merge(
169+
$this->generateForSpecificStoreView($id, $productCategories, $scopedProduct, $rootCategoryId)
170+
);
171+
}
156172
}
157173
}
158174

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CatalogUrlRewrite\Test\Unit\Model;
99

1010
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Catalog\Model\Category;
1213
use Magento\Catalog\Model\Product;
1314
use Magento\CatalogUrlRewrite\Model\ObjectRegistry;
@@ -73,6 +74,9 @@ class ProductScopeRewriteGeneratorTest extends TestCase
7374
/** @var CategoryRepositoryInterface|MockObject */
7475
private $categoryRepositoryMock;
7576

77+
/** @var ProductRepositoryInterface|MockObject */
78+
private $productRepositoryMock;
79+
7680
protected function setUp(): void
7781
{
7882
$this->serializer = $this->createMock(Json::class);
@@ -131,6 +135,7 @@ function ($value) {
131135
->getMock();
132136

133137
$this->categoryRepositoryMock = $this->getMockForAbstractClass(CategoryRepositoryInterface::class);
138+
$this->productRepositoryMock = $this->getMockForAbstractClass(ProductRepositoryInterface::class);
134139

135140
$this->productScopeGenerator = (new ObjectManager($this))->getObject(
136141
ProductScopeRewriteGenerator::class,
@@ -144,7 +149,8 @@ function ($value) {
144149
'storeManager' => $this->storeManager,
145150
'mergeDataProviderFactory' => $mergeDataProviderFactory,
146151
'config' => $this->configMock,
147-
'categoryRepository' => $this->categoryRepositoryMock
152+
'categoryRepository' => $this->categoryRepositoryMock,
153+
'productRepository' =>$this->productRepositoryMock
148154
]
149155
);
150156
$this->categoryMock = $this->getMockBuilder(Category::class)
@@ -161,7 +167,7 @@ public function testGenerationForGlobalScope()
161167
$product->expects($this->any())->method('getStoreId')->willReturn(null);
162168
$product->expects($this->any())->method('getStoreIds')->willReturn([1]);
163169
$this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore')
164-
->willReturn(false);
170+
->willReturn(true);
165171
$this->initObjectRegistryFactory([]);
166172
$canonical = new UrlRewrite([], $this->serializer);
167173
$canonical->setRequestPath('category-1')
@@ -185,6 +191,7 @@ public function testGenerationForGlobalScope()
185191
->setStoreId(4);
186192
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
187193
->willReturn([$anchorCategories]);
194+
$this->productRepositoryMock->expects($this->once())->method('getById')->willReturn($product);
188195

189196
$this->assertEquals(
190197
[
@@ -230,19 +237,6 @@ public function testGenerationForSpecificStore()
230237
);
231238
}
232239

233-
/**
234-
* Test method
235-
*/
236-
public function testSkipGenerationForGlobalScope()
237-
{
238-
$product = $this->createMock(Product::class);
239-
$product->expects($this->any())->method('getStoreIds')->willReturn([1, 2]);
240-
$this->storeViewService->expects($this->exactly(2))->method('doesEntityHaveOverriddenUrlKeyForStore')
241-
->willReturn(true);
242-
243-
$this->assertEquals([], $this->productScopeGenerator->generateForGlobalScope([], $product, 1));
244-
}
245-
246240
/**
247241
* @param array $entities
248242
*/

0 commit comments

Comments
 (0)