0

I'm debugging url rewrites in M2.

  • One can change the product url key in catalog/product/edit in the Search Engine Optimization tab
  • I'm looking at Magento\Catalog\Controller\Adminhtml\Product\Save::execute yet I see nothing related to url keys.

  • Q: Who is in charge with of url_rewrites CURD? Where are url_rewrites handled during product update?

2 Answers 2

0

Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::getUrlPath() Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::getUrlPath()

0

There is two way to change product URL one is create plugin

of Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::getUrlPath()

Create a Observer for product save after event

create events.xml at below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

create Productsaveafter.php at below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $_product->setUrlKey($url);
        $_product->save();    
    }   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.