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.

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();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.