Wednesday, July 15, 2020
Home > Beginners of Magento 2 > How to get Product URL from product object in Magento 2?

How to get Product URL from product object in Magento 2?

In Magento 2, every product have unique url and for that, you can get product url from product object.
You can use product url anywhere if you want to redirect by one page to another.

Here is the some ways to get product object which will return product url and other information which we you want.

Get Product Object by Sku or Id in Magento 2.

Write Block to get product object:

Everyone can use a block class of the module Ketan_GetProduct and inject the object productRepository of \Magento\Catalog\Model\ProductRepository class in the constructor of the module’s block class.

app/code/Ketan/GetProduct/Block/GetProduct.php

Get product id and sku in template file:

app/code/Ketan/GetProduct/view/frontend/template/getproduct.phtml

Next, please use the below script to get product by id and sku in the template file.

Spread the love

Leave a Reply

Your email address will not be published.