Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top