I am reading a core function in Magento 2.3, the function is
\Magento\Wishlist\Helper\Data::getProductUrl
* Retrieve URL to item Product
*
* @param \Magento\Wishlist\Model\Item|\Magento\Catalog\Model\Product $item
* @param array $additional
* @return string
*/
public function getProductUrl($item, $additional = [])
{
if ($item instanceof \Magento\Catalog\Model\Product) {
$product = $item;
} else {
$product = $item->getProduct();
}
$buyRequest = $item->getBuyRequest();
if (is_object($buyRequest)) {
$config = $buyRequest->getSuperProductConfig();
if ($config && !empty($config['product_id'])) {
$product = $this->productRepository->getById(
$config['product_id'],
false,
$this->_storeManager->getStore()->getStoreId()
);
}
}
return $product->getUrlModel()->getUrl($product, $additional);
}
I am surprised to see the function calls the product repository to load the product url. I'd like to hear from people who perform code reviews/optimisation and understand whether this practice to call product repository is recommended or to be cautiously practised?
In this function, we see 2 use cases and one does not call repository. But yet this function can be called in a loop and my question is very much whether we potentially could run into page load issue or whether the cache does the rest?
thanks for looking into this