1

In Magento 2.2.3, I am using the following for loop to get the product image. When I print that image url, it takes the previous product's URL actually it doesnt have the URL.

 $products = $this->getCollection ();
    foreach ( $products as $product ) {

        $preImageUrl = $product->getImage ();
}
1
  • Please check my answer and upvote accept if it's helpful for you. Commented Jul 6, 2018 at 4:13

1 Answer 1

2

You can also get full image path of product using this code :

protected $_product;
protected $_storeManager;

public function __construct(
 ...
\Magento\Catalog\Model\Product $product,
\Magento\Store\Model\StoreManagerInterface $storeManager,
 ...
)
{
...
$this->_product = $product;
$this->$_storeManager = $storeManager;
...
}

public function execute(){
    $product = $this->_product->load('product_id');
    $mediaPath = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $product_image_full_path =  $mediaPath . 'catalog/product' . $product->getImage();
}

Hope, it will useful for you !!

1
  • hi @Rohan, this will return the product image without cache, same image how can we load in list page, default magento category page loads images with cache folder Commented Jun 7, 2019 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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