File tree 4 files changed +85
-4
lines changed
app/code/Magento/CatalogUrlRewrite 4 files changed +85
-4
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Copyright © Magento, Inc. All rights reserved.
4
+ * See COPYING.txt for license details.
5
+ */
6
+ namespace Magento \CatalogUrlRewrite \Observer ;
7
+
8
+ use Magento \Catalog \Model \Product ;
9
+ use Magento \Framework \Event \ObserverInterface ;
10
+ use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
11
+
12
+ /**
13
+ * Class ProductSaveRewritesHistorySetterObserver
14
+ *
15
+ * @package Magento\CatalogUrlRewrite\Observer
16
+ */
17
+ class ProductSaveRewritesHistorySetterObserver implements ObserverInterface
18
+ {
19
+ /**
20
+ * @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
21
+ */
22
+ private $ productUrlPathGenerator ;
23
+
24
+ /**
25
+ * @param ProductUrlPathGenerator $productUrlPathGenerator
26
+ */
27
+ public function __construct (ProductUrlPathGenerator $ productUrlPathGenerator )
28
+ {
29
+ $ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
30
+ }
31
+
32
+ /**
33
+ * Always set save_rewrites_history to 1 for API calls so there will always be created a SEO rewrite for the old url
34
+ *
35
+ * @param \Magento\Framework\Event\Observer $observer
36
+ * @return void
37
+ */
38
+ public function execute (\Magento \Framework \Event \Observer $ observer )
39
+ {
40
+ /**
41
+ * @var Product $product
42
+ */
43
+ $ product = $ observer ->getEvent ()->getProduct ();
44
+ $ urlKey = $ this ->productUrlPathGenerator ->getUrlKey ($ product );
45
+ if (null !== $ urlKey ) {
46
+ $ product ->setSaveRewritesHistory (1 );
47
+ }
48
+ }
49
+ }
Original file line number Diff line number Diff line change 7
7
8
8
use Magento \Catalog \Model \Product ;
9
9
use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
10
- use Magento \Framework \Event \Observer ;
11
10
use Magento \Framework \Event \ObserverInterface ;
12
11
12
+ /**
13
+ * Class ProductUrlKeyAutogeneratorObserver
14
+ *
15
+ * @package Magento\CatalogUrlRewrite\Observer
16
+ */
13
17
class ProductUrlKeyAutogeneratorObserver implements ObserverInterface
14
18
{
15
19
/**
16
20
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
17
21
*/
18
- protected $ productUrlPathGenerator ;
22
+ private $ productUrlPathGenerator ;
19
23
20
24
/**
21
25
* @param ProductUrlPathGenerator $productUrlPathGenerator
@@ -26,12 +30,16 @@ public function __construct(ProductUrlPathGenerator $productUrlPathGenerator)
26
30
}
27
31
28
32
/**
29
- * @param \Magento\Framework\Event\Observer $observer
33
+ * Generate url_key and set it on the Product
34
+ *
35
+ * @param \Magento\Framework\Event\Observer $observer
30
36
* @return void
31
37
*/
32
38
public function execute (\Magento \Framework \Event \Observer $ observer )
33
39
{
34
- /** @var Product $product */
40
+ /**
41
+ * @var Product $product
42
+ */
35
43
$ product = $ observer ->getEvent ()->getProduct ();
36
44
$ urlKey = $ this ->productUrlPathGenerator ->getUrlKey ($ product );
37
45
if (null !== $ urlKey ) {
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" ?>
2
+ <!--
3
+ /**
4
+ * Copyright © Magento, Inc. All rights reserved.
5
+ * See COPYING.txt for license details.
6
+ */
7
+ -->
8
+ <config xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation =" urn:magento:framework:Event/etc/events.xsd" >
9
+ <event name =" catalog_product_save_before" >
10
+ <observer name =" product_save_rewrites_history_setter" instance =" Magento\CatalogUrlRewrite\Observer\ProductSaveRewritesHistorySetterObserver" />
11
+ </event >
12
+ </config >
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" ?>
2
+ <!--
3
+ /**
4
+ * Copyright © Magento, Inc. All rights reserved.
5
+ * See COPYING.txt for license details.
6
+ */
7
+ -->
8
+ <config xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : noNamespaceSchemaLocation =" urn:magento:framework:Event/etc/events.xsd" >
9
+ <event name =" catalog_product_save_before" >
10
+ <observer name =" product_save_rewrites_history_setter" instance =" Magento\CatalogUrlRewrite\Observer\ProductSaveRewritesHistorySetterObserver" />
11
+ </event >
12
+ </config >
You can’t perform that action at this time.
0 commit comments