55 */
66namespace Magento \CatalogUrlRewrite \Observer ;
77
8- use Magento \Catalog \Api \Data \ProductInterface ;
98use 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 ;
139use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
1410use 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 ;
1812use Magento \UrlRewrite \Model \UrlPersistInterface ;
1913use Magento \Framework \Event \ObserverInterface ;
20- use Magento \Store \Model \StoreManagerInterface ;
2114
2215/**
2316 * 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.
2817 */
2918class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
3019{
@@ -43,62 +32,30 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
4332 */
4433 private $ productUrlPathGenerator ;
4534
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-
6635 /**
6736 * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
6837 * @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
7439 */
7540 public function __construct (
7641 ProductUrlRewriteGenerator $ productUrlRewriteGenerator ,
7742 UrlPersistInterface $ urlPersist ,
78- ProductUrlPathGenerator $ productUrlPathGenerator ,
79- StoreManagerInterface $ storeManager ,
80- ProductScopeRewriteGenerator $ productScopeRewriteGenerator ,
81- DeleteEntitiesFromStores $ deleteEntitiesFromStores ,
82- CollectionFactory $ collectionFactory
43+ ProductUrlPathGenerator $ productUrlPathGenerator = null
8344 ) {
8445 $ this ->productUrlRewriteGenerator = $ productUrlRewriteGenerator ;
8546 $ 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);
9149 }
9250
9351 /**
9452 * Generate urls for UrlRewrite and save it in storage
9553 *
96- * @param Observer $observer
54+ * @param \Magento\Framework\Event\ Observer $observer
9755 * @return void
98- * @throws UrlAlreadyExistsException
99- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
56+ * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
10057 */
101- public function execute (Observer $ observer )
58+ public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
10259 {
10360 /** @var Product $product */
10461 $ product = $ observer ->getEvent ()->getProduct ();
@@ -108,49 +65,11 @@ public function execute(Observer $observer)
10865 || $ product ->getIsChangedWebsites ()
10966 || $ product ->dataHasChangedFor ('visibility ' )
11067 ) {
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 ));
11571 $ this ->urlPersist ->replace ($ this ->productUrlRewriteGenerator ->generate ($ product ));
11672 }
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- }
15473 }
15574 }
15675}
0 commit comments