1

I have to display an image of the ordered product. I am using getAllVisibleItems() to get all the details of my order.

From where I can get my ordered product image.

1
  • vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml Feb 13 '19 at 13:14
0

You can use below method:

Get the sku of the item as below

$dataHelper = $this->helper('Vendor\Module\Helper\Data');
foreach ($order->getAllVisibleItems() as $item) {  
$sku = $item->getSku();
$image = $dataHelper->getProductImageUrl($sku);
}

Your helper class Vendor/Module/Helper/Data.php

<?php

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Data extends AbstractHelper
{

    public function __construct(
        Context $context,

        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Catalog\Helper\Image $imageHelper
        )
    {
        $this->productRepository = $productRepository;
        $this->imageHelper = $imageHelper;
        parent::__construct($context);
    }

    public function getProductBySku($sku) {
        return $this->productRepository->get($sku);
    }
    public function getProductImageUrl($sku) {
        $image_url = '';
        $product = $this->getProductBySku($sku);

        if (isset($product)) {
           $image_url = $this->imageHelper->init($product, 'product_page_image_medium')->setImageFile($product->getImage())->resize(480, 600)->getUrl();
        } 
        return $image_url;
    }
}

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.