Skip to content

Commit a73b032

Browse files
committed
ACP2E-1783: Unable to create 301/302 redirect for product with a category path when Generate "category/product" URL Rewrites set to Yes
- refactored solution
1 parent ad11c85 commit a73b032

File tree

3 files changed

+54
-259
lines changed

3 files changed

+54
-259
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Products/AppendUrlRewritesToProducts.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,53 +79,38 @@ public function __construct(
7979
public function execute(array $products, array $storesToAdd): void
8080
{
8181
foreach ($products as $product) {
82-
$urls = $this->getProductUrlRewrites($product, $storesToAdd);
82+
$forceGenerateDefault = false;
83+
foreach ($storesToAdd as $storeId) {
84+
if ($this->needGenerateUrlForStore($product, (int)$storeId)) {
85+
$urls[] = $this->generateUrls($product, (int)$storeId);
86+
} elseif ((int)$product->getStoreId() !== Store::DEFAULT_STORE_ID) {
87+
$forceGenerateDefault = true;
88+
}
89+
}
90+
if ($product->getStoreId() === Store::DEFAULT_STORE_ID
91+
|| $this->isProductAssignedToStore($product)) {
92+
$product->unsUrlPath();
93+
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
94+
$urls[] = $this->productUrlRewriteGenerator->generate($product);
95+
}
96+
if ($forceGenerateDefault && $product->getStoreId() !== Store::DEFAULT_STORE_ID) {
97+
$urls[] = $this->generateUrls($product, Store::DEFAULT_STORE_ID);
98+
}
8399
$this->getDataByStore->clearProductUrlRewriteDataCache($product);
84100
}
85101
if (!empty($urls)) {
86102
$this->urlPersist->replace(array_merge(...$urls));
87103
}
88104
}
89105

90-
/**
91-
* Generate store product URLs
92-
*
93-
* @param ProductInterface $product
94-
* @param array $stores
95-
* @return array
96-
*/
97-
public function getProductUrlRewrites(ProductInterface $product, array $stores): array
98-
{
99-
$urls = [];
100-
$forceGenerateDefault = false;
101-
foreach ($stores as $storeId) {
102-
if ($this->needGenerateUrlForStore($product, (int)$storeId)) {
103-
$urls[] = $this->generateProductStoreUrls($product, (int)$storeId);
104-
} elseif ((int)$product->getStoreId() !== Store::DEFAULT_STORE_ID) {
105-
$forceGenerateDefault = true;
106-
}
107-
}
108-
if ($product->getStoreId() === Store::DEFAULT_STORE_ID
109-
|| $this->isProductAssignedToStore($product)) {
110-
$product->unsUrlPath();
111-
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
112-
$urls[] = $this->productUrlRewriteGenerator->generate($product);
113-
}
114-
if ($forceGenerateDefault && $product->getStoreId() !== Store::DEFAULT_STORE_ID) {
115-
$urls[] = $this->generateProductStoreUrls($product, Store::DEFAULT_STORE_ID);
116-
}
117-
118-
return $urls;
119-
}
120-
121106
/**
122107
* Generate urls for specific store
123108
*
124109
* @param ProductInterface $product
125110
* @param int $storeId
126111
* @return array
127112
*/
128-
private function generateProductStoreUrls(ProductInterface $product, int $storeId): array
113+
private function generateUrls(ProductInterface $product, int $storeId): array
129114
{
130115
$storeData = $this->getDataByStore->execute($product, $storeId);
131116
$origStoreId = $product->getStoreId();

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Products/AppendUrlRewritesToProductsTest.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)