5
5
*/
6
6
namespace Magento \CatalogUrlRewrite \Observer ;
7
7
8
- use Magento \Catalog \Api \Data \ProductInterface ;
9
8
use Magento \Catalog \Model \Product ;
10
- use Magento \Catalog \Model \Product \Visibility ;
11
- use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
12
- use Magento \CatalogUrlRewrite \Model \ProductScopeRewriteGenerator ;
13
9
use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
14
10
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
15
- use Magento \Framework \Event \Observer ;
16
- use Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException ;
17
- use Magento \UrlRewrite \Model \Storage \DeleteEntitiesFromStores ;
11
+ use Magento \Framework \App \ObjectManager ;
18
12
use Magento \UrlRewrite \Model \UrlPersistInterface ;
19
13
use Magento \Framework \Event \ObserverInterface ;
20
- use Magento \Store \Model \StoreManagerInterface ;
21
14
22
15
/**
23
16
* Class ProductProcessUrlRewriteSavingObserver
24
- *
25
- * Observer to update the Rewrite URLs for a product.
26
- * This observer is triggered on the save function when making changes
27
- * to the products website on the Product Edit page.
28
17
*/
29
18
class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
30
19
{
@@ -43,62 +32,30 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
43
32
*/
44
33
private $ productUrlPathGenerator ;
45
34
46
- /**
47
- * @var StoreManagerInterface
48
- */
49
- private $ storeManager ;
50
-
51
- /**
52
- * @var ProductScopeRewriteGenerator
53
- */
54
- private $ productScopeRewriteGenerator ;
55
-
56
- /**
57
- * @var DeleteEntitiesFromStores
58
- */
59
- private $ deleteEntitiesFromStores ;
60
-
61
- /**
62
- * @var CollectionFactory
63
- */
64
- private $ collectionFactory ;
65
-
66
35
/**
67
36
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
68
37
* @param UrlPersistInterface $urlPersist
69
- * @param ProductUrlPathGenerator $productUrlPathGenerator
70
- * @param StoreManagerInterface $storeManager
71
- * @param ProductScopeRewriteGenerator $productScopeRewriteGenerator
72
- * @param DeleteEntitiesFromStores $deleteEntitiesFromStores
73
- * @param CollectionFactory $collectionFactory
38
+ * @param ProductUrlPathGenerator|null $productUrlPathGenerator
74
39
*/
75
40
public function __construct (
76
41
ProductUrlRewriteGenerator $ productUrlRewriteGenerator ,
77
42
UrlPersistInterface $ urlPersist ,
78
- ProductUrlPathGenerator $ productUrlPathGenerator ,
79
- StoreManagerInterface $ storeManager ,
80
- ProductScopeRewriteGenerator $ productScopeRewriteGenerator ,
81
- DeleteEntitiesFromStores $ deleteEntitiesFromStores ,
82
- CollectionFactory $ collectionFactory
43
+ ProductUrlPathGenerator $ productUrlPathGenerator = null
83
44
) {
84
45
$ this ->productUrlRewriteGenerator = $ productUrlRewriteGenerator ;
85
46
$ this ->urlPersist = $ urlPersist ;
86
- $ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
87
- $ this ->storeManager = $ storeManager ;
88
- $ this ->productScopeRewriteGenerator = $ productScopeRewriteGenerator ;
89
- $ this ->deleteEntitiesFromStores = $ deleteEntitiesFromStores ;
90
- $ this ->collectionFactory = $ collectionFactory ;
47
+ $ this ->productUrlPathGenerator = $ productUrlPathGenerator ?: ObjectManager::getInstance ()
48
+ ->get (ProductUrlPathGenerator::class);
91
49
}
92
50
93
51
/**
94
52
* Generate urls for UrlRewrite and save it in storage
95
53
*
96
- * @param Observer $observer
54
+ * @param \Magento\Framework\Event\ Observer $observer
97
55
* @return void
98
- * @throws UrlAlreadyExistsException
99
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
56
+ * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
100
57
*/
101
- public function execute (Observer $ observer )
58
+ public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
102
59
{
103
60
/** @var Product $product */
104
61
$ product = $ observer ->getEvent ()->getProduct ();
@@ -108,49 +65,11 @@ public function execute(Observer $observer)
108
65
|| $ product ->getIsChangedWebsites ()
109
66
|| $ product ->dataHasChangedFor ('visibility ' )
110
67
) {
111
- //Refresh rewrite urls
112
- $ product ->unsUrlPath ();
113
- $ product ->setUrlPath ($ this ->productUrlPathGenerator ->getUrlPath ($ product ));
114
- if (!empty ($ this ->productUrlRewriteGenerator ->generate ($ product ))) {
68
+ if ($ product ->isVisibleInSiteVisibility ()) {
69
+ $ product ->unsUrlPath ();
70
+ $ product ->setUrlPath ($ this ->productUrlPathGenerator ->getUrlPath ($ product ));
115
71
$ this ->urlPersist ->replace ($ this ->productUrlRewriteGenerator ->generate ($ product ));
116
72
}
117
-
118
- $ storeIdsToRemove = [];
119
- $ productWebsiteMap = array_flip ($ product ->getWebsiteIds ());
120
- $ storeVisibilities = $ this ->collectionFactory ->create ()
121
- ->getAllAttributeValues (ProductInterface::VISIBILITY );
122
- if ($ this ->productScopeRewriteGenerator ->isGlobalScope ($ product ->getStoreId ())) {
123
- //Remove any rewrite URLs for websites the product is not in, or is not visible in. Global Scope.
124
- foreach ($ this ->storeManager ->getStores () as $ store ) {
125
- $ websiteId = $ store ->getWebsiteId ();
126
- $ storeId = $ store ->getStoreId ();
127
- if (!isset ($ productWebsiteMap [$ websiteId ])) {
128
- $ storeIdsToRemove [] = $ storeId ;
129
- continue ;
130
- }
131
- //Check the visibility of the product in each store.
132
- if (isset ($ storeVisibilities [$ product ->getId ()][$ storeId ])
133
- && ($ storeVisibilities [$ product ->getId ()][$ storeId ] === Visibility::VISIBILITY_NOT_VISIBLE )) {
134
- $ storeIdsToRemove [] = $ storeId ;
135
- }
136
- }
137
- } else {
138
- //Only remove rewrite for current scope
139
- $ websiteId = $ product ->getStore ()->getWebsiteId ();
140
- $ storeId = $ product ->getStoreId ();
141
- if (!isset ($ productWebsiteMap [$ websiteId ]) ||
142
- (isset ($ storeVisibilities [$ product ->getId ()][$ storeId ])
143
- && ($ storeVisibilities [$ product ->getId ()][$ storeId ] === Visibility::VISIBILITY_NOT_VISIBLE ))) {
144
- $ storeIdsToRemove [] = $ storeId ;
145
- }
146
- }
147
- if (count ($ storeIdsToRemove )) {
148
- $ this ->deleteEntitiesFromStores ->execute (
149
- $ storeIdsToRemove ,
150
- [$ product ->getId ()],
151
- ProductUrlRewriteGenerator::ENTITY_TYPE
152
- );
153
- }
154
73
}
155
74
}
156
75
}
0 commit comments