9

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?

3 Answers 3

16

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
20
  • 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? Commented Feb 17, 2017 at 12:35
  • try this code $_item->getProduct()->getThumbnail(); Commented Feb 17, 2017 at 12:40
  • shall i echo it like echo $_item->getProduct()->getThumbnail() in both default.html and items.html file Commented Feb 17, 2017 at 16:48
  • 2
    no sir i will manage to get extra points..lets hope i'll manage to achieve this Commented Feb 23, 2017 at 6:42
  • 1
    @Vikram $imageHelper = $objectManager->get('\Magento\Catalog\Helper\Product'); echo $imageHelper->getThumbnailUrl($product); will do that Commented Jan 31, 2018 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; ?>
39
  • 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.? Commented Feb 17, 2017 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
    Commented Feb 17, 2017 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? Commented Feb 17, 2017 at 12:17
  • I have tried code suggested by you and qaiser sir but no result :( Commented Feb 19, 2017 at 5:44
  • any reply please? Commented Feb 21, 2017 at 8:03
0

You can try following approach to convert existing image as Thumbnail

public function getProductThumbnailImages($product)
{
    $product = $this->productRepository->getById($product->getId());
    $mediaGalleryImages = $product->getMediaGalleryImages();
    $maxThumbnails = $this->maxThumbnails();
    $imagewidth = $this->getImageOriginalWidth($product, 'category_page_grid'); //Get image width from theme -> etc/view.xml
    $imageheight = $this->getImageOriginalHeight($product, 'category_page_grid'); //Get image height from theme -> etc/view.xml
    $thumbnails = [];
    
    //Iterate through each gallery image
    foreach ($mediaGalleryImages as $mediaGalleryEntry)
    {
        //Check if thumbnail carousel flag is enabled on this image
        if($mediaGalleryEntry->getThumbnailCarousel())
        {
            //Get Image URL of the Product's Gallery image after resizing for Thumbnail
            $image_url = $this->imageHelper->init($product, 'product_small_image')
                            ->constrainOnly(true)    //If set true, then smaller images will not be enlarged
                            ->keepAspectRatio(true)  //If set true the proportional relationship is consistent for width and height
                            ->keepTransparency(true) //It is specified as the default value of the Transparent background image
                            ->keepFrame(false)       //If set true then the images will not be cropped
                            ->setImageFile($mediaGalleryEntry->getFile())
                            ->resize($imagewidth, $imageheight)
                            ->getUrl();

            $thumbnails[] = $image_url;
        }

        //Check number of thumbnails
        if(count($thumbnails) >= $maxThumbnails)
        {
            break; //Exit loop
        }
    }

    //Return product thumbnails
    return $thumbnails;
}

/**
 * Retrieve image width
 *
 * @return int|null
 */
public function getImageOriginalWidth($product, $imageId, $attributes = [])
{
    return $this->imageHelper->init($product, $imageId, $attributes)->getWidth();
}

/**
 * Retrieve image height
 *
 * @return int|null
 */
public function getImageOriginalHeight($product, $imageId, $attributes = [])
{
    return $this->imageHelper->init($product, $imageId, $attributes)->getHeight();
}

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.