13
13
use Magento \Store \Model \Store ;
14
14
use Magento \UrlRewrite \Model \UrlPersistInterface ;
15
15
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
16
+ use Magento \Store \Api \StoreWebsiteRelationInterface ;
17
+ use Magento \Framework \App \ObjectManager ;
16
18
17
19
/**
18
20
* Observer to assign the products to website
@@ -39,22 +41,31 @@ class ProductToWebsiteChangeObserver implements ObserverInterface
39
41
*/
40
42
protected $ request ;
41
43
44
+ /**
45
+ * @var StoreWebsiteRelationInterface
46
+ */
47
+ private $ storeWebsiteRelation ;
48
+
42
49
/**
43
50
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
44
51
* @param UrlPersistInterface $urlPersist
45
52
* @param ProductRepositoryInterface $productRepository
46
53
* @param RequestInterface $request
54
+ * @param StoreWebsiteRelationInterface $storeWebsiteRelation
47
55
*/
48
56
public function __construct (
49
57
ProductUrlRewriteGenerator $ productUrlRewriteGenerator ,
50
58
UrlPersistInterface $ urlPersist ,
51
59
ProductRepositoryInterface $ productRepository ,
52
- RequestInterface $ request
60
+ RequestInterface $ request ,
61
+ StoreWebsiteRelationInterface $ storeWebsiteRelation = null
53
62
) {
54
63
$ this ->productUrlRewriteGenerator = $ productUrlRewriteGenerator ;
55
64
$ this ->urlPersist = $ urlPersist ;
56
65
$ this ->productRepository = $ productRepository ;
57
66
$ this ->request = $ request ;
67
+ $ this ->storeWebsiteRelation = $ storeWebsiteRelation ?:
68
+ ObjectManager::getInstance ()->get (StoreWebsiteRelationInterface::class);
58
69
}
59
70
60
71
/**
@@ -73,10 +84,17 @@ public function execute(\Magento\Framework\Event\Observer $observer)
73
84
);
74
85
75
86
if (!empty ($ this ->productUrlRewriteGenerator ->generate ($ product ))) {
76
- $ this ->urlPersist ->deleteByData ([
77
- UrlRewrite::ENTITY_ID => $ product ->getId (),
78
- UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE ,
79
- ]);
87
+ if ($ this ->request ->getParam ('remove_website_ids ' )) {
88
+ foreach ($ this ->request ->getParam ('remove_website_ids ' ) as $ webId ) {
89
+ foreach ($ this ->storeWebsiteRelation ->getStoreByWebsiteId ($ webId ) as $ storeId ) {
90
+ $ this ->urlPersist ->deleteByData ([
91
+ UrlRewrite::ENTITY_ID => $ product ->getId (),
92
+ UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE ,
93
+ UrlRewrite::STORE_ID => $ storeId
94
+ ]);
95
+ }
96
+ }
97
+ }
80
98
if ($ product ->getVisibility () != Visibility::VISIBILITY_NOT_VISIBLE ) {
81
99
$ this ->urlPersist ->replace ($ this ->productUrlRewriteGenerator ->generate ($ product ));
82
100
}
0 commit comments