Skip to content

[Forwardport] Module Catalog URL Rewrite: fix issue with product URL Rewrites re-generation after changing product URL Key for product with existing url_path attribute value #18711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
namespace Magento\CatalogUrlRewrite\Observer;

use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Framework\App\ObjectManager;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Framework\Event\ObserverInterface;

/**
* Class ProductProcessUrlRewriteSavingObserver
*/
class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
{
/**
Expand All @@ -23,22 +27,33 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
*/
private $urlPersist;

/**
* @var ProductUrlPathGenerator
*/
private $productUrlPathGenerator;

/**
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param UrlPersistInterface $urlPersist
* @param ProductUrlPathGenerator|null $productUrlPathGenerator
*/
public function __construct(
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
UrlPersistInterface $urlPersist
UrlPersistInterface $urlPersist,
ProductUrlPathGenerator $productUrlPathGenerator = null
) {
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->urlPersist = $urlPersist;
$this->productUrlPathGenerator = $productUrlPathGenerator ?: ObjectManager::getInstance()
->get(ProductUrlPathGenerator::class);
}

/**
* Generate urls for UrlRewrite and save it in storage
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
Expand All @@ -51,6 +66,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
|| $product->dataHasChangedFor('visibility')
) {
if ($product->isVisibleInSiteVisibility()) {
$product->unsUrlPath();
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
$this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
}
}
Expand Down