Skip to content

Commit 2a856b3

Browse files
committed
ACP2E-808: Url Rewrite is not regenerated for store view with overridden url_key after switching visibility from "Not Visible Individually" on global scope
1 parent 5ab2114 commit 2a856b3

File tree

4 files changed

+296
-121
lines changed

4 files changed

+296
-121
lines changed

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Model\Product\Visibility;
1111
use Magento\CatalogUrlRewrite\Model\Products\AppendUrlRewritesToProducts;
1212
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
13+
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1314
use Magento\Framework\App\Config\ScopeConfigInterface;
1415
use Magento\Framework\Event\Observer;
1516
use Magento\Framework\Event\ObserverInterface;
@@ -46,22 +47,30 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
4647
*/
4748
private $getStoresList;
4849

50+
/**
51+
* @var StoreViewService
52+
*/
53+
private $storeViewService;
54+
4955
/**
5056
* @param UrlPersistInterface $urlPersist
51-
* @param AppendUrlRewritesToProducts|null $appendRewrites
57+
* @param AppendUrlRewritesToProducts $appendRewrites
5258
* @param ScopeConfigInterface $scopeConfig
5359
* @param GetStoresListByWebsiteIds $getStoresList
60+
* @param StoreViewService $storeViewService
5461
*/
5562
public function __construct(
5663
UrlPersistInterface $urlPersist,
5764
AppendUrlRewritesToProducts $appendRewrites,
5865
ScopeConfigInterface $scopeConfig,
59-
GetStoresListByWebsiteIds $getStoresList
66+
GetStoresListByWebsiteIds $getStoresList,
67+
StoreViewService $storeViewService
6068
) {
6169
$this->urlPersist = $urlPersist;
6270
$this->appendRewrites = $appendRewrites;
6371
$this->scopeConfig = $scopeConfig;
6472
$this->getStoresList = $getStoresList;
73+
$this->storeViewService = $storeViewService;
6574
}
6675

6776
/**
@@ -82,6 +91,23 @@ public function execute(Observer $observer)
8291
$storesToAdd = $this->getStoresList->execute(
8392
array_diff($product->getWebsiteIds(), $oldWebsiteIds)
8493
);
94+
95+
if ($product->getStoreId() === Store::DEFAULT_STORE_ID
96+
&& $product->dataHasChangedFor('visibility')
97+
&& (int) $product->getOrigData('visibility') === Visibility::VISIBILITY_NOT_VISIBLE
98+
) {
99+
foreach ($product->getStoreIds() as $storeId) {
100+
if (!$this->storeViewService->doesEntityHaveOverriddenVisibilityForStore(
101+
$storeId,
102+
$product->getId(),
103+
Product::ENTITY
104+
)
105+
) {
106+
$storesToAdd[] = $storeId;
107+
}
108+
}
109+
$storesToAdd = array_unique($storesToAdd);
110+
}
85111
$this->appendRewrites->execute([$product], $storesToAdd);
86112
}
87113
}
@@ -102,8 +128,21 @@ private function deleteObsoleteRewrites(Product $product): void
102128
array_diff($oldWebsiteIds, $product->getWebsiteIds())
103129
);
104130
if ((int)$product->getVisibility() === Visibility::VISIBILITY_NOT_VISIBLE) {
105-
$isGlobalScope = $product->getStoreId() == Store::DEFAULT_STORE_ID;
106-
$storesToRemove[] = $isGlobalScope ? $product->getStoreIds() : $product->getStoreId();
131+
if ($product->getStoreId() === Store::DEFAULT_STORE_ID) {
132+
foreach ($product->getStoreIds() as $storeId) {
133+
if (!$this->storeViewService->doesEntityHaveOverriddenVisibilityForStore(
134+
$storeId,
135+
$product->getId(),
136+
Product::ENTITY
137+
)
138+
) {
139+
$storesToRemove[] = $storeId;
140+
}
141+
}
142+
} else {
143+
$storesToRemove[] = $product->getStoreId();
144+
}
145+
$storesToRemove = array_unique($storesToRemove);
107146
}
108147
if ($storesToRemove) {
109148
$this->urlPersist->deleteByData(
@@ -130,7 +169,6 @@ private function isWebsiteChanged(Product $product)
130169
return array_diff($oldWebsiteIds, $newWebsiteIds) || array_diff($newWebsiteIds, $oldWebsiteIds);
131170
}
132171

133-
134172
/**
135173
* Is product rewrites need to be updated
136174
*

app/code/Magento/CatalogUrlRewrite/Service/V1/StoreViewService.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Catalog\Api\Data\ProductInterface;
1212

1313
/**
14-
* Store view service
14+
* Service that checks scope overridden values
1515
*/
1616
class StoreViewService
1717
{
@@ -70,6 +70,20 @@ public function doesEntityHaveOverriddenUrlPathForStore($storeId, $entityId, $en
7070
return $this->doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entityId, $entityType, 'url_path');
7171
}
7272

73+
/**
74+
* Check that entity has overridden visibility for specific store
75+
*
76+
* @param int $storeId
77+
* @param int $entityId
78+
* @param string $entityType
79+
* @throws \InvalidArgumentException
80+
* @return bool
81+
*/
82+
public function doesEntityHaveOverriddenVisibilityForStore($storeId, $entityId, $entityType)
83+
{
84+
return $this->doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entityId, $entityType, 'visibility');
85+
}
86+
7387
/**
7488
* Check that entity has overridden url attribute for specific store
7589
*

0 commit comments

Comments
 (0)