6
6
namespace Magento \CatalogUrlRewrite \Observer ;
7
7
8
8
use Magento \Catalog \Model \Product ;
9
+ use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
9
10
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
11
+ use Magento \Framework \App \ObjectManager ;
10
12
use Magento \UrlRewrite \Model \UrlPersistInterface ;
11
- use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
12
13
use Magento \Framework \Event \ObserverInterface ;
13
14
15
+ /**
16
+ * Class ProductProcessUrlRewriteSavingObserver
17
+ */
14
18
class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
15
19
{
16
20
/**
@@ -24,21 +28,32 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
24
28
private $ urlPersist ;
25
29
26
30
/**
27
- * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
28
- * @param UrlPersistInterface $urlPersist
31
+ * @var ProductUrlPathGenerator
32
+ */
33
+ private $ productUrlPathGenerator ;
34
+
35
+ /**
36
+ * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
37
+ * @param UrlPersistInterface $urlPersist
38
+ * @param ProductUrlPathGenerator|null $productUrlPathGenerator
29
39
*/
30
40
public function __construct (
31
41
ProductUrlRewriteGenerator $ productUrlRewriteGenerator ,
32
- UrlPersistInterface $ urlPersist
42
+ UrlPersistInterface $ urlPersist ,
43
+ ProductUrlPathGenerator $ productUrlPathGenerator = null
33
44
) {
34
45
$ this ->productUrlRewriteGenerator = $ productUrlRewriteGenerator ;
35
46
$ this ->urlPersist = $ urlPersist ;
47
+ $ this ->productUrlPathGenerator = $ productUrlPathGenerator ?: ObjectManager::getInstance ()
48
+ ->get (ProductUrlPathGenerator::class);
36
49
}
37
50
38
51
/**
39
52
* Generate urls for UrlRewrite and save it in storage
53
+ *
40
54
* @param \Magento\Framework\Event\Observer $observer
41
55
* @return void
56
+ * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
42
57
*/
43
58
public function execute (\Magento \Framework \Event \Observer $ observer )
44
59
{
@@ -51,6 +66,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
51
66
|| $ product ->dataHasChangedFor ('visibility ' )
52
67
) {
53
68
if ($ product ->isVisibleInSiteVisibility ()) {
69
+ $ product ->unsUrlPath ();
70
+ $ product ->setUrlPath ($ this ->productUrlPathGenerator ->getUrlPath ($ product ));
54
71
$ this ->urlPersist ->replace ($ this ->productUrlRewriteGenerator ->generate ($ product ));
55
72
}
56
73
}
0 commit comments