Skip to content

Commit 20ea9fe

Browse files
committed
ACP2E-1754: Store level URL rewrites are removed after product import
1 parent 56f2bb6 commit 20ea9fe

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ class AfterImportDataObserver implements ObserverInterface
195195
*/
196196
private $attributeValue;
197197

198+
/**
199+
* @var null|array
200+
*/
201+
private $cachedValues = null;
202+
198203
/**
199204
* @param ProductFactory $catalogProductFactory
200205
* @param ObjectRegistryFactory $objectRegistryFactory
@@ -455,28 +460,29 @@ private function isGlobalScope($storeId)
455460
private function canonicalUrlRewriteGenerate(array $products)
456461
{
457462
$urls = [];
458-
$cachedValues = null;
459463
foreach ($products as $productId => $productsByStores) {
460464
foreach ($productsByStores as $storeId => $product) {
461465
if ($this->productUrlPathGenerator->getUrlPath($product)) {
462466
$reqPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId);
467+
$targetPath = $this->productUrlPathGenerator->getCanonicalUrlPath($product);
463468
if ((int) $storeId !== (int) $product->getStoreId()
464469
&& $this->isGlobalScope($product->getStoreId())) {
465-
if ($cachedValues === null) {
466-
$cachedValues = $this->getScopeBasedUrlKeyValues($products);
470+
if ($this->cachedValues === null) {
471+
$this->cachedValues = $this->getScopeBasedUrlKeyValues($products);
467472
}
468-
if (!empty($cachedValues) && isset($cachedValues[$productId][$storeId])) {
473+
if (!empty($this->cachedValues) && isset($this->cachedValues[$productId][$storeId])) {
469474
$storeProduct = clone $product;
470475
$storeProduct->setStoreId($storeId);
471-
$storeProduct->setUrlKey($cachedValues[$productId][$storeId]);
476+
$storeProduct->setUrlKey($this->cachedValues[$productId][$storeId]);
472477
$reqPath = $this->productUrlPathGenerator->getUrlPathWithSuffix($storeProduct, $storeId);
478+
$targetPath = $this->productUrlPathGenerator->getCanonicalUrlPath($storeProduct);
473479
}
474480
}
475481
$urls[] = $this->urlRewriteFactory->create()
476482
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
477483
->setEntityId($productId)
478484
->setRequestPath($reqPath)
479-
->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product))
485+
->setTargetPath($targetPath)
480486
->setStoreId($storeId);
481487
}
482488
}
@@ -637,6 +643,20 @@ private function generateForCustom(UrlRewrite $url, ?Category $category, array $
637643
$targetPath = $url->getRedirectType()
638644
? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category)
639645
: $url->getTargetPath();
646+
if ((int) $storeId !== (int) $product->getStoreId()
647+
&& $this->isGlobalScope($product->getStoreId())) {
648+
if ($this->cachedValues === null) {
649+
$this->cachedValues = $this->getScopeBasedUrlKeyValues($products);
650+
}
651+
if (!empty($this->cachedValues) && isset($this->cachedValues[$productId][$storeId])) {
652+
$storeProduct = clone $product;
653+
$storeProduct->setStoreId($storeId);
654+
$storeProduct->setUrlKey($this->cachedValues[$productId][$storeId]);
655+
$targetPath = $url->getRedirectType()
656+
? $this->productUrlPathGenerator->getUrlPathWithSuffix($storeProduct, $storeId, $category)
657+
: $url->getTargetPath();
658+
}
659+
}
640660
if ($url->getRequestPath() === $targetPath) {
641661
return [];
642662
}

0 commit comments

Comments
 (0)