0

I want to rewrite the url of product from

www.example.com/product-name.html

To

www.example.com/SKU.html

Someone can help me? The products we sell are unique, but share the same name. So when we add two different products with the same name, it throws back the error that that URL already exists.

1 Answer 1

1

Please created a custom module to override

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

Where in prepareProductUrlKey()

From

return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() : $urlKey);

To

return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() . "-" . $product->getSku() : $urlKey);

Thanks

2
  • Hi Mayur, After changing the above code, existing product url key will work.
    – gill
    Commented Sep 16, 2020 at 3:58
  • I checked this in local and it is working so give you. Commented Sep 16, 2020 at 5:28

Your Answer

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

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