1010use Magento \Catalog \Model \Product \Visibility ;
1111use Magento \CatalogUrlRewrite \Model \Products \AppendUrlRewritesToProducts ;
1212use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
13+ use Magento \CatalogUrlRewrite \Service \V1 \StoreViewService ;
1314use Magento \Framework \App \Config \ScopeConfigInterface ;
1415use Magento \Framework \Event \Observer ;
1516use 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 *
0 commit comments