Skip to content

Commit 5bcc411

Browse files
committed
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-3900' into PR_2025_06_25_muntianu
2 parents 7bdd10b + 9d71ab9 commit 5bcc411

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Product/CurrentUrlRewritesRegenerator.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Model\Category;
99
use Magento\Catalog\Model\CategoryRepository;
1010
use Magento\Catalog\Model\Product;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
1112
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1213
use Magento\UrlRewrite\Model\OptionProvider;
1314
use Magento\CatalogUrlRewrite\Model\ObjectRegistry;
@@ -18,6 +19,8 @@
1819
use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder;
1920
use Magento\Framework\App\ObjectManager;
2021
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
22+
use Magento\CatalogUrlRewrite\Block\UrlKeyRenderer;
23+
use Magento\Store\Model\ScopeInterface;
2124

2225
/**
2326
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -27,18 +30,21 @@ class CurrentUrlRewritesRegenerator
2730
/**
2831
* @var Product
2932
* @deprecated 100.1.0
33+
* @see not used
3034
*/
3135
protected $product;
3236

3337
/**
3438
* @var ObjectRegistry
3539
* @deprecated 100.1.0
40+
* @see not used
3641
*/
3742
protected $productCategories;
3843

3944
/**
4045
* @var UrlFinderInterface
4146
* @deprecated 100.1.0
47+
* @see not used
4248
*/
4349
protected $urlFinder;
4450

@@ -72,21 +78,28 @@ class CurrentUrlRewritesRegenerator
7278
*/
7379
private $categoryRepository;
7480

81+
/**
82+
* @var ScopeConfigInterface
83+
*/
84+
private ScopeConfigInterface $scopeConfig;
85+
7586
/**
7687
* @param UrlFinderInterface $urlFinder
7788
* @param ProductUrlPathGenerator $productUrlPathGenerator
7889
* @param UrlRewriteFactory $urlRewriteFactory
7990
* @param UrlRewriteFinder|null $urlRewriteFinder
8091
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
8192
* @param CategoryRepository|null $categoryRepository
93+
* @param ScopeConfigInterface|null $scopeConfig
8294
*/
8395
public function __construct(
8496
UrlFinderInterface $urlFinder,
8597
ProductUrlPathGenerator $productUrlPathGenerator,
8698
UrlRewriteFactory $urlRewriteFactory,
8799
?UrlRewriteFinder $urlRewriteFinder = null,
88100
?MergeDataProviderFactory $mergeDataProviderFactory = null,
89-
?CategoryRepository $categoryRepository = null
101+
?CategoryRepository $categoryRepository = null,
102+
?ScopeConfigInterface $scopeConfig = null
90103
) {
91104
$this->urlFinder = $urlFinder;
92105
$this->productUrlPathGenerator = $productUrlPathGenerator;
@@ -98,6 +111,7 @@ public function __construct(
98111
}
99112
$this->categoryRepository = $categoryRepository ?: ObjectManager::getInstance()->get(CategoryRepository::class);
100113
$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();
114+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
101115
}
102116

103117
/**
@@ -119,6 +133,14 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate
119133
$rootCategoryId
120134
);
121135

136+
$isSaveHistory = $this->scopeConfig->isSetFlag(
137+
UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY,
138+
ScopeInterface::SCOPE_STORE,
139+
$storeId
140+
);
141+
142+
$product->setData('save_rewrites_history', $isSaveHistory);
143+
122144
foreach ($currentUrlRewrites as $currentUrlRewrite) {
123145
$category = $this->retrieveCategoryFromMetadata($currentUrlRewrite, $productCategories);
124146
if ($category === false) {
@@ -158,10 +180,12 @@ public function generateAnchor(
158180
ProductUrlRewriteGenerator::ENTITY_TYPE,
159181
$rootCategoryId
160182
);
161-
162-
foreach ($productCategories->getList() as $productCategory) {
163-
$anchorCategoryIds = array_merge($productCategory->getAnchorsAbove(), $anchorCategoryIds);
164-
}
183+
$anchorCategoryIds = array_merge(
184+
...array_map(
185+
fn($productCategory) => $productCategory->getAnchorsAbove(),
186+
$productCategories->getList()
187+
)
188+
);
165189

166190
foreach ($currentUrlRewrites as $currentUrlRewrite) {
167191
$metadata = $currentUrlRewrite->getMetadata();
@@ -181,6 +205,8 @@ public function generateAnchor(
181205
}
182206

183207
/**
208+
* Generate URL rewrites for autogenerated URLs
209+
*
184210
* @param UrlRewrite $url
185211
* @param int $storeId
186212
* @param Category|null $category
@@ -209,6 +235,8 @@ protected function generateForAutogenerated($url, $storeId, $category, $product
209235
}
210236

211237
/**
238+
* Generate URL rewrites for custom URLs
239+
*
212240
* @param UrlRewrite $url
213241
* @param int $storeId
214242
* @param Category|null $category
@@ -237,6 +265,8 @@ protected function generateForCustom($url, $storeId, $category, $product = null)
237265
}
238266

239267
/**
268+
* Retrieve category from URL metadata
269+
*
240270
* @param UrlRewrite $url
241271
* @param ObjectRegistry|null $productCategories
242272
* @return Category|null|bool

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* * All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -278,8 +278,11 @@ public function testProductUrlRewritePerStoreViews(): void
278278
$product
279279
);
280280
$urlRewriteItems = $this->getEntityRewriteCollection($product->getId())->getItems();
281-
$this->assertTrue(count($urlRewriteItems) == 2);
281+
$this->assertTrue(count($urlRewriteItems) == 3);
282282
foreach ($urlRewriteItems as $item) {
283+
if ((int) $item->getRedirectType() == OptionProvider::PERMANENT) {
284+
continue;
285+
}
283286
$item->getData('store_id') == $secondStoreId
284287
? $this->assertEquals($urlKeySecondStore . $this->suffix, $item->getRequestPath())
285288
: $this->assertEquals($urlKeyFirstStore . $this->suffix, $item->getRequestPath());

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandlerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ function (UrlRewrite $urlRewrite) {
8282

8383
$expected = [
8484
'store-1-key.html',
85-
'cat-1/store-1-key.html'
85+
'cat-1/store-1-key.html',
86+
'p002.html'
8687
];
8788
self::assertEquals($expected, $actual, 'Generated URLs rewrites do not match.');
8889
}
@@ -118,16 +119,17 @@ function (UrlRewrite $urlRewrite) {
118119
$generatedUrls
119120
)
120121
);
122+
sort($actual);
121123

122124
$expected = [
123-
'store-1-key.html', // the Default store
124-
'store-1-key.html', // the Secondary store
125+
'cat-1-2/p002.html', // the Secondary store with Category URL key, first store view
126+
'cat-1-2/p002.html', // the Secondary store with Category URL key, second store view
125127
'cat-1/store-1-key.html', // the Default store with Category URL key, first store view
126128
'cat-1/store-1-key.html', // the Default store with Category URL key, second store view
127-
'p002.html', // the Default store
128129
'p002.html', // the Secondary store
129-
'cat-1-2/p002.html', // the Secondary store with Category URL key, first store view
130-
'cat-1-2/p002.html', // the Secondary store with Category URL key, second store view
130+
'p002.html', // the Default store
131+
'store-1-key.html', // the Default store
132+
'store-1-key.html', // the Secondary store
131133
];
132134
self::assertEquals($expected, $actual, 'Generated URLs rewrites do not match.');
133135
}

0 commit comments

Comments
 (0)