How to Get Product Image URL in Magento 2
You may require to import product images in Magento 2 while migrating the website data or require to get the product image URL to fetch product images, implement custom functionality or developing some features. Today, I’ve come up with the solution to get product image URL in Magento 2.
Steps to get product image URL in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace namespace Vendor\Extension\Block; use Magento\Framework\View\Element\Template; use Magento\Catalog\Helper\Image; use Magento\Catalog\Model\ProductFactory; use Magento\Framework\View\Element\Template\Context; class Extension extends Template { protected $imageHelper; protected $productFactory; public function __construct(Image $imageHelper, ProductFactory $productFactory, Context,) { $this->imageHelper = $imageHelper; $this->productFactory = $productFactory; } public function getProductImageUrl($id) { try { $product = $this->productFactory->create()->load($id); } catch (NoSuchEntityException $e) { return 'Data not found'; } $url = $this->imageHelper->init($product, 'product_thumbnail_image')->getUrl(); return $url; } } |
Also Read Change Product URL in Shopify
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Can I share a link?
Hello, please can you elaborate?