I want to get the product URL with categories in home page where I just load the product collection and used the getProductUrl()
function.
But it returns only product URL key.
1 Answer
First, you have to create an instance of ObjectManager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
Then, load the product by product id i.e., pass the id
of the corresponding product
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);
Now call the getProductUrl() function and it will return the URL of your product
echo $product->getProductUrl();
This method will return the URL of your product.
-
Yeah!! I did the same thing what you said! But I want the url with its category. Now it is returning only product url. Commented May 29, 2017 at 14:29