2

I am trying to show product image in order emails.

for that i overrided below file to my custom theme to implement it.

Magento_Sales/templates/email/items/order/default.phtml

$_order = $_item->getOrder();
$_store = $_order->getStore();
$_imageHelper = \Magento\Framework\App\ObjectManager::getInstance()- >get('Magento\Catalog\Helper\Image');
$_baseImageUrl = $_store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product';

$productRepository = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Api\ProductRepositoryInterface');
$productSku = $block->getSku($_item);
$productObj = $productRepository->get($productSku);
$imageUrl = $_imageHelper->init($productObj, 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('65','65')->getUrl();

Then after the line

<p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>

I added my code like below.

  <p><img src="<?php echo $imageUrl; ?>" alt="Product Image"></p>    

This is showing always src as unknown.

But the image is there.

1

You can try below code

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productId = 1;
    $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
    // $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
    // $image_url = $imageHelper->init($_product, 'product_page_image_small')->setImageFile($_product->getImage())->resize(100, 100)->getUrl();

    $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');

    $rootPath = $directory->getPath('media');
    $imageUrl = $rootPath."/catalog/product".$_product->getImage();

    <img src="<?php echo $imageUrl; ?>" />

Note :- The code will work, but it is best practice to not reference the ObjectManager class directly.

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.