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?

  • 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 Feb 26 '17 at 20:01
  • cant i achieve it programmatically? – Yogesh Raghav Feb 27 '17 at 3:58
  • i want to know that how to achieve this functionality pro grammatically and which file i need to change for this? – Yogesh Raghav Feb 27 '17 at 5:21
  • i have tried manually from seo url rewites but its not working but is there any programming trick to achieve this? – Yogesh Raghav Feb 27 '17 at 6:25
  • any reply please? – Yogesh Raghav Feb 27 '17 at 8:30
3

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.

  • 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 – Yogesh Raghav Feb 27 '17 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 Feb 27 '17 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? – Yogesh Raghav Feb 27 '17 at 10:36
  • do i have to override observer class here? – Yogesh Raghav Feb 27 '17 at 11:08
  • Please see the example: github – sergei.sss Feb 27 '17 at 19:21

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.