4

I want to change my Magento 2 product urlkey and set something like example.com/product-name/product-id instead of example.com/product-name.html.

Where do I have to change it while saving product from admin ?

I found this file but it does not work .

vendor\magento\module-catalog-url-rewrite\Model\ProductUrlPathGenerator.php

I changed in this file and then created a new product. Should I change another file? Please help me.

2

The URL key is generated by an Observer that uses the class you've mentionned:

app/code/Magento/CatalogUrlRewrite/Observer/ProductUrlKeyAutogeneratorObserver :

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var Product $product */
    $product = $observer->getEvent()->getProduct();
    $product->setUrlKey($this->productUrlPathGenerator->getUrlKey($product));
}

Then in the ProductUrlGenerator you have those two methods:

public function getUrlKey($product)
{
    return $product->getUrlKey() === false ? false : $this->prepareProductUrlKey($product);
}

protected function prepareProductUrlKey(\Magento\Catalog\Model\Product $product)
{
    $urlKey = $product->getUrlKey();
    return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() : $urlKey);
}

My suggestion to implement what you want would be to implement a plugin via a custom module

In your di.xml you can add the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator">
        <plugin name="custom_url_generation" type="Vendor\Module\Plugin\CustomUrlGenerator"/>
    </type>
</config>

Then in Plugin\CustomUrlGenerator.php:

<?php

namespace Vendor\Module\Plugin;

class CustomUrlGenerator {

    public function beforeGetUrlKey(\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $subject, $product)
    {
         $urlKey = $product->getUrlKey();
         if ($urlKey === '' || $urlKey === null) {
              $product->setUrlKey($product->getName() . "/" . $product->getId());
         }
         return $product;
    }
}
  • Thanks but one problem is arise . when I print "$product->getId()" in beforegetUrlKey() function it return null because in this function product will not saved . how to set product id in url key ? I also tried catalog_product_save_after but problem is when I use save() function in catalog_product_save_after observer page is infinite loading .not response from browser and not display product page – Niraj Patel Jun 30 '16 at 11:10
1

I know this is an old thread, but I needed this code to add custom URL's to my webshop. The answer Raphael at Digital Pianism gave is correct, except for the return $product; statement. Using this will give an Intereceptor error.

To fix this, replace return $product; with return ['getUrlKey' => $product];

1

Raphael at Digital Pianism was right. Looks like the class has been changed as of Magento 2.2.4 to return the string.

I solved it by using an around plugin and returning the $process.

public function aroundGetUrlKey(\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $subject, callable $process, \Magento\Catalog\Model\Product $product)
{
    $urlKey = $product ? $product->getUrlKey() : '';
    if ($urlKey === '' || $urlKey === null) {
        $product->setUrlKey($product->getName() . "/" . $product->getId());
    }
    return $process($product);
}
0

Magento 2 transliter URL using filters that remove anything that is not alphanumeric. I started the same way using a plugin to go around the GetUrlKey, however, even if the url is saved with a slash in it, the generated entry in the core URL entry will have the slash removed.

I just submitted a pull request https://github.com/magento/magento2/pull/17478 that should allow slashes and underscores in the URL rewrite. Let's see if it's accepted. But if so, then @JonShipman answer will work. If you're in a hurry you can take the pull request and create an override for the TranslitUrl filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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