5

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?

8

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
|improve this answer|||||
  • 1
    i have tried above steps. i have put the code in /vendor/magento/module-checkout/view/frontend/templates/succ‌​ess.html added $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product')->loa‌​d($item->getProductI‌​d()); <?php echo $product->getThumbnail(); echo $_product->getProductUrl(); ?> in both default.phtml and items.phtml but iam not getting any results. this is a screenshot of my confirmation email. prntscr.com/e9vsx7 wherer am i going wrong?plz explain where i have to put these codes? – Yogesh Raghav Feb 17 '17 at 12:35
  • try this code $_item->getProduct()->getThumbnail(); – Qaisar Satti Feb 17 '17 at 12:40
  • shall i echo it like echo $_item->getProduct()->getThumbnail() in both default.html and items.html file – Yogesh Raghav Feb 17 '17 at 16:48
  • 2
    no sir i will manage to get extra points..lets hope i'll manage to achieve this – Yogesh Raghav Feb 23 '17 at 6:42
  • 1
    @Vikram $imageHelper = $objectManager->get('\Magento\Catalog\Helper\Product'); echo $imageHelper->getThumbnailUrl($product); will do that – Qaisar Satti Jan 31 '18 at 10:14
2

I've tried this code myself, its working.

override vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml and use this code in it:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Sales\Block\Order\Email\Items\DefaultItems */

/** @var $_item \Magento\Sales\Model\Order\Item */
$_item = $block->getItem();
$_order = $_item->getOrder();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
?>
<tr>
    <td class="item-info<?php if ($block->getItemOptions()): ?> has-extra<?php endif; ?>">
        <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
        <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
        <p class="sku"><?= /* @escapeNotVerified */  __('SKU'); ?>: <?= $block->escapeHtml($block->getSku($_item)) ?></p>
        <?php if ($block->getItemOptions()): ?>
            <dl class="item-options">
            <?php foreach ($block->getItemOptions() as $option): ?>
                <dt><strong><em><?= /* @escapeNotVerified */  $option['label'] ?></em></strong></dt>
                <dd>
                    <?= /* @escapeNotVerified */  nl2br($option['value']) ?>
                </dd>
            <?php endforeach; ?>
            </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?= $addInfoBlock->setItem($_item)->toHtml() ?>
        <?php endif; ?>
        <?= $block->escapeHtml($_item->getDescription()) ?>
    </td>
    <td>
        <?php
            $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($_item->getProductId());

        $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');

        $image_url = $imageHelper->init($_product, 'product_page_image_small')->setImageFile($_product->getFile())->resize(100, 100)->getUrl();
        ?>
        <img src="<?php echo $image_url; ?>">
    </td>
    <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
    <td class="item-price">
        <?= /* @escapeNotVerified */  $block->getItemPrice($_item); ?>
    </td>
</tr>
<?php if ($_item->getGiftMessageId() && $_giftMessage = $this->helper('Magento\GiftMessage\Helper\Message')->getGiftMessage($_item->getGiftMessageId())): ?>
<tr>
    <td colspan="3" class="item-extra">
        <table class="message-gift">
            <tr>
                <td>
                    <h3><?= /* @escapeNotVerified */  __('Gift Message') ?></h3>
                    <strong><?= /* @escapeNotVerified */  __('From:'); ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                    <br /><strong><?= /* @escapeNotVerified */  __('To:'); ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                    <br /><strong><?= /* @escapeNotVerified */  __('Message:'); ?></strong>
                    <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                </td>
            </tr>
        </table>
    </td>
</tr>
<?php endif; ?>

and in override vendor/magento/module-sales/view/frontend/templates/email/it‌​ems.phtml and use:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php $_order = $block->getOrder() ?>
<?php if ($_order): ?>
    <?php $_items = $_order->getAllItems(); ?>
    <table class="email-items">
        <thead>
            <tr>
                <th class="item-info">
                    <?= /* @escapeNotVerified */  __('Items'); ?>
                </th>
                <th class="item-qty">
                    <?= /* @escapeNotVerified */  __('Qty'); ?>
                </th>
                <th class="item-qty">
                    <?= /* @escapeNotVerified */  __('Image'); ?>
                </th>
                <th class="item-price">
                    <?= /* @escapeNotVerified */  __('Price'); ?>
                </th>
            </tr>
        </thead>
        <?php foreach ($_items as $_item): ?>
            <?php
                if ($_item->getParentItem()) {
                    continue;
                }
            ?>
            <tbody>
                <?= $block->getItemHtml($_item) ?>
            </tbody>
        <?php endforeach; ?>
        <tfoot class="order-totals">
            <?= $block->getChildHtml('order_totals') ?>
        </tfoot>
    </table>
    <?php if ($this->helper('Magento\GiftMessage\Helper\Message')->isMessagesAllowed('order', $_order, $_order->getStore()) && $_order->getGiftMessageId()): ?>
        <?php $_giftMessage = $this->helper('Magento\GiftMessage\Helper\Message')->getGiftMessage($_order->getGiftMessageId()); ?>
        <?php if ($_giftMessage): ?>
            <br />
            <table class="message-gift">
                <tr>
                    <td>
                        <h3><?= /* @escapeNotVerified */  __('Gift Message for this Order') ?></h3>
                        <strong><?= /* @escapeNotVerified */  __('From:'); ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                        <br /><strong><?= /* @escapeNotVerified */  __('To:'); ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                        <br /><strong><?= /* @escapeNotVerified */  __('Message:'); ?></strong>
                        <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                    </td>
                </tr>
            </table>
        <?php endif; ?>
    <?php endif; ?>
<?php endif; ?>
|improve this answer|||||
  • so can i use same code in default.phtml and items.phtml both for simple product and configurable product or i have to change some coding in both file?can u please brief this.? – Yogesh Raghav Feb 17 '17 at 11:26
  • I had answered according to magento2. Now I've updated the answer. You can use this code in default.phtml and items.phtml but don't forget that first two lines are for loading the order, so that could vary. – Manish Joy Feb 17 '17 at 11:34
  • i have tried above steps. i have put the code in /vendor/magento/module-checkout/view/frontend/templates/success.html added $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId()); <?php echo $product->getThumbnail(); echo $_product->getProductUrl(); ?> in both default.phtml and items.phtml but iam not getting any results. this is a screenshot of my confirmation email. prntscr.com/e9vsx7 wherer am i going wrong?plz explain where i have to put these codes? – Yogesh Raghav Feb 17 '17 at 12:17
  • I have tried code suggested by you and qaiser sir but no result :( – Yogesh Raghav Feb 19 '17 at 5:44
  • any reply please? – Yogesh Raghav Feb 21 '17 at 8:03

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.