Skip to content

Commit a1de534

Browse files
committed
[TASK] Always setSaveRewritesHistory to 1 for API calls so there will be created a SEO rewrite for the old url
1 parent 80469a6 commit a1de534

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

app/code/Magento/CatalogUrlRewrite/Observer/ProductUrlKeyAutogeneratorObserver.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77

88
use Magento\Catalog\Model\Product;
99
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
10-
use Magento\Framework\Event\Observer;
1110
use Magento\Framework\Event\ObserverInterface;
1211

12+
/**
13+
* Class ProductUrlKeyAutogeneratorObserver
14+
*
15+
* @package Magento\CatalogUrlRewrite\Observer
16+
*/
1317
class ProductUrlKeyAutogeneratorObserver implements ObserverInterface
1418
{
1519
/**
1620
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
1721
*/
18-
protected $productUrlPathGenerator;
22+
private $productUrlPathGenerator;
1923

2024
/**
2125
* @param ProductUrlPathGenerator $productUrlPathGenerator
@@ -26,12 +30,16 @@ public function __construct(ProductUrlPathGenerator $productUrlPathGenerator)
2630
}
2731

2832
/**
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
3036
* @return void
3137
*/
3238
public function execute(\Magento\Framework\Event\Observer $observer)
3339
{
34-
/** @var Product $product */
40+
/**
41+
* @var Product $product
42+
*/
3543
$product = $observer->getEvent()->getProduct();
3644
$urlKey = $this->productUrlPathGenerator->getUrlKey($product);
3745
if (null !== $urlKey) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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>

0 commit comments

Comments
 (0)