0

I am using magnto2.1.3. i want to show my product id in url on product listing page.below is what i need. enter image description here

how to achieve this?kindly help me please?

5
  • You can add ID to Url-key manually for each product. Or use third-party extensions such as SEO Suite Template for automatically updating product URL keys using pattern. But, in both cases your product URLs will be replaced anywhere, not only in the product list pages.
    – sergei.sss
    Commented Feb 26, 2017 at 20:01
  • cant i achieve it programmatically? Commented Feb 27, 2017 at 3:58
  • i want to know that how to achieve this functionality pro grammatically and which file i need to change for this? Commented Feb 27, 2017 at 5:21
  • i have tried manually from seo url rewites but its not working but is there any programming trick to achieve this? Commented Feb 27, 2017 at 6:25
  • any reply please? Commented Feb 27, 2017 at 8:30

1 Answer 1

4

Magento add URL-rewrite for product here: Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver

In your extension you should add "before" plugin for execute method. See magento tutorial

In your plugin method you should add:

$product->setUrlKey($product->getUrlKey() . '_' . $product->getId());

(Of course, also you can add some checks for exists product ID in URL key for avoid [url_key]_[id]_[id]_[id]

P.S. This method will not modify URL-key attribute (because product was saved before), but will modify URL rewrite.
P.S.2. You can use around plugin and after creating URL rewrite set original URL-key for product for avoid some conflicts.

34
  • can you please explain in detail like what initially i have to start with.iam newbie to magento2.1.3 so dont have much idea about customization....can you please explain in brief like how i suppose to start?for a newbie like me it would be really helpful. thank in advance Commented Feb 27, 2017 at 8:47
  • I won't be able to tell about creation of the extension and a plug-in it is better, than a official manual. If you have specific question you can ask me.
    – sergei.sss
    Commented Feb 27, 2017 at 9:00
  • ok i have created a custom module name yogesh\ProductClass now which class file i should override here?is it ProductProcessUrlRewriteSavingObserver.php.if yes shall i create __construct function and write my code in order to use that code and the second step that u mentioned is to set original url key for product to avoid conflicts.i mean whats that and how to achieve this? Commented Feb 27, 2017 at 10:36
  • do i have to override observer class here? Commented Feb 27, 2017 at 11:08
  • Please see the example: github
    – sergei.sss
    Commented Feb 27, 2017 at 19:21

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.