1

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

1

You should try and avoid individually loading products but instead load them as part of a larger product collection.

However see getbyid second argument. This improves performance for loading if you are not editing. This invokes a cache like mechanism.

| improve this answer | |

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.