1

In my block I have provide a function to fetch product details from product Id. Code in block file

 public function getProductById($pid) 
{ 
    $product = $this->_productRepository->getById($pid);
    return $product->getUrlModel()->getUrl($product);
}

And in .phtml file I have provided

  $returnProdId = $block->getProductById($returnItems['products']);
  print_r($returnProdId);

But the result is not as expected.Result which I am getting is http://localhost/magento2/index.php/catalog/product/view/id/1420/s/iris-workout-top-xs-red/

Please help on the same.

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Bridgit Thomas
  • 165
  • 5
  • 16

1 Answers1

5

Can you please try by replacing below

 public function getProductById($pid) 
{ 
    $product = $this->_productRepository->getById($pid);
    return $product->getUrlModel()->getUrl($product);
}

With

public function getProductById($pid) 
    { 
        $product = $this->_productRepository->getById($pid);
        return $product->getProductUrl();
    }
Jeya Kiruthika
  • 406
  • 3
  • 10