Magento 2.1 – How to Get Product Thumbnail and URL in Order Confirmation Email

configurable-productmagento-2.1order-confirmationproduct-imagessimple-product

I have tried already mentioned solution for getting product thumbnail image along with product url (link to product)using below solution

/vendor/magento/module-sales/view/frontend/templates/order/items/renderer/default.phtml

<?php echo $_item->getThumbnail(); ?>

<?php echo $this->$_item->getThumbnail()); ?>

<?php echo Mage::getModel('catalog/product_media_config')->getMediaUrl($_item->getThumbnail()); ?>

and on /vendor/magento/module-sales/view/frontend/templates/order/items.html

<?php $_order = $block->getOrder() ?>
<!--Custom-->
<?php $_item = $block->getItem() ?>
<!--Custom-->
<?php $_giftMessage; ?>
<div class="table-wrapper order-items">
    <table class="data table table-order-items" id="my-orders-table" summary="<?php /* @escapeNotVerified */ echo __('Items Ordered') ?>">
        <caption class="table-caption"><?php /* @escapeNotVerified */ echo __('Items Ordered') ?></caption>
        <thead>
            <tr>
                <th class="col name"><?php echo $this->__('Image')?></th> //<-This code
                <th class="col name"><?php /* @escapeNotVerified */ echo __('Product Name') ?></th>
                <p class="product-name"><a href="<?php $_item->getProduct()->getProductUrl(); ?>"> <?= $block->escapeHtml($_item->getName()) ?></a></p>
                <th class="col sku"><?php /* @escapeNotVerified */ echo __('SKU') ?></th>
                <!--Product Image 17-02-17 -->
                <th class="col price"><?php /* @escapeNotVerified */ echo __('Price') ?></th>
                <th class="col qty"><?php /* @escapeNotVerified */ echo __('Qty') ?></th>
                <th class="col subtotal"><?php /* @escapeNotVerified */ echo __('Subtotal') ?></th>
            </tr>
        </thead>
        <?php $_items = $_order->getItemsCollection(); ?>
        <?php $_index = 0; ?>
        <?php $_count = $_items->count(); ?>
        <?php $_giftMessage = ''?>
        <?php foreach ($_items as $_item): ?>
            <?php if ($_item->getParentItem()) {
    continue;
} ?>

but not getting either of product url and product image on order confirmation mail when i tries testing from my end.any solutions?

Best Answer

you have to load product to get thumbnail and url .

    <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProduct()->getId());
 echo $product->getThumbnail();
echo  $product->getProductUrl(); ?>

//get image url

$imagewidth=200;
$imageheight=200;
$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl();
 echo $image_url
Related Topic