Hello Magento Pals 👋,
How you all doing? Today’s illustration is on How to Get Product Image URL in Magento 2. In case you missed out on our previous blog, take a look, Magento 2: How To Redirect 404 Page Found To Search Page.
Images are an easy option to improve the user experience of any E-commerce website. It helps to grab the customer’s attention and get them engaged. Every product of an E-commerce website holds different images.
While implementing any new feature related to images you may require a product image URL. Moreover, at the time of migrating your Magento 2 store, you need to get Image URLs of all the products. The below code will help you to Get Product Image URL in Magento 2.
So let’s get started 🚀
Contents
One can retrieve the Image URL using two methods:
protected $_productloader; protected $_storeManager; public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productrepository, \Magento\Store\Model\StoreManagerInterface $storemanager) { $this->productrepository = $productrepository; $this->_storeManager = $storemanager; } Public function getProductImageUsingCode() { $store = $this->_storeManager->getStore(); $productId = 95; $product = $this->productrepository->getById($productid); $productImageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' .$product->getImage(); $productUrl = $product->getProductUrl(); return $productUrl; }
$prdId = 35; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productid); $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(); $productImageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage(); $productUrl = $product->getProductUrl();
I hope you have successfully applied the above-mentioned steps and you are able to Get Product Image URL in Magento 2. By chance, if you face any difficulties write to me in the comment section below. Do not forget to rate and share the article.
Happy Coding 😊
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…
Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…
Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…
View Comments
how to add type image type for width & heignt from view.xml
For that you need to do custom code according to your requirements.
Is there some di.xml configuration missing from this article - after I added the code in the first example my page broke.
$prdoduct should be $product
Thank you for pointing the typo, its updated now.