0

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 1

0

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.

1
  • Yeah!! I did the same thing what you said! But I want the url with its category. Now it is returning only product url.
    – Murali Raj
    Commented May 29, 2017 at 14:29

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.