10
10
use Magento \Catalog \Model \Product \Visibility ;
11
11
use Magento \CatalogUrlRewrite \Model \Products \AppendUrlRewritesToProducts ;
12
12
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
13
+ use Magento \CatalogUrlRewrite \Service \V1 \StoreViewService ;
13
14
use Magento \Framework \App \Config \ScopeConfigInterface ;
14
15
use Magento \Framework \Event \Observer ;
15
16
use Magento \Framework \Event \ObserverInterface ;
@@ -46,22 +47,30 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
46
47
*/
47
48
private $ getStoresList ;
48
49
50
+ /**
51
+ * @var StoreViewService
52
+ */
53
+ private $ storeViewService ;
54
+
49
55
/**
50
56
* @param UrlPersistInterface $urlPersist
51
- * @param AppendUrlRewritesToProducts|null $appendRewrites
57
+ * @param AppendUrlRewritesToProducts $appendRewrites
52
58
* @param ScopeConfigInterface $scopeConfig
53
59
* @param GetStoresListByWebsiteIds $getStoresList
60
+ * @param StoreViewService $storeViewService
54
61
*/
55
62
public function __construct (
56
63
UrlPersistInterface $ urlPersist ,
57
64
AppendUrlRewritesToProducts $ appendRewrites ,
58
65
ScopeConfigInterface $ scopeConfig ,
59
- GetStoresListByWebsiteIds $ getStoresList
66
+ GetStoresListByWebsiteIds $ getStoresList ,
67
+ StoreViewService $ storeViewService
60
68
) {
61
69
$ this ->urlPersist = $ urlPersist ;
62
70
$ this ->appendRewrites = $ appendRewrites ;
63
71
$ this ->scopeConfig = $ scopeConfig ;
64
72
$ this ->getStoresList = $ getStoresList ;
73
+ $ this ->storeViewService = $ storeViewService ;
65
74
}
66
75
67
76
/**
@@ -82,6 +91,23 @@ public function execute(Observer $observer)
82
91
$ storesToAdd = $ this ->getStoresList ->execute (
83
92
array_diff ($ product ->getWebsiteIds (), $ oldWebsiteIds )
84
93
);
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
+ }
85
111
$ this ->appendRewrites ->execute ([$ product ], $ storesToAdd );
86
112
}
87
113
}
@@ -102,8 +128,21 @@ private function deleteObsoleteRewrites(Product $product): void
102
128
array_diff ($ oldWebsiteIds , $ product ->getWebsiteIds ())
103
129
);
104
130
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 );
107
146
}
108
147
if ($ storesToRemove ) {
109
148
$ this ->urlPersist ->deleteByData (
@@ -130,7 +169,6 @@ private function isWebsiteChanged(Product $product)
130
169
return array_diff ($ oldWebsiteIds , $ newWebsiteIds ) || array_diff ($ newWebsiteIds , $ oldWebsiteIds );
131
170
}
132
171
133
-
134
172
/**
135
173
* Is product rewrites need to be updated
136
174
*
0 commit comments