8

I want to add an image of the product in the mail send after a successful checkout.

I am so far that I made a module that overrides

vendor/magento/module-sales/view/frontend/templates/email/items.phtml

But how to insert an image thumbnail?

items.phtml

<?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-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; ?>
12

I did that by copying vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml to my template folder app/design/frontend/[VENDOR]/[TEMPLATE]/Magento_Sales/templates/email/items/order/default.phtml and adding the image column there...

$_item = $block->getItem();
$_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';

and right below

<img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('65','65')->getUrl();?>" alt="<?= __('Product Image');?>">
  • Do I insert the <img src= code right below the $_baseImageUrl = code? The reason I ask is that I'm getting a syntax error when I do and just wanted to make sure? Thanks for the help. – Rudy C. Oct 25 '18 at 17:03
  • What kind of syntax error are you having? (remember to enable short_open_tag on php.ini) – Ricardo Martins Oct 25 '18 at 22:31
0

Actually, I did not tried in magento-2 version. But i tried in Magento-1.9 version. I got the result.

Please go through Product Thumbnail in sales-order emails this link of magento connect manager for product thumbnail image in order emails.

  • This is something i want, but not compatible with magento 2.... – Jilco Tigchelaar Nov 28 '16 at 10:07
  • No solution yet.... – Jilco Tigchelaar Nov 30 '16 at 14:25
  • A change in the default.phtml dit not influence the email at all. Also if i change it in the root folder manually nothing happend. Items.phmtl is what i need to influence, and that's working now. But how to get an image there... – Jilco Tigchelaar Nov 30 '16 at 14:46
0

I'll admit that this is bad form, and it should be refactored into a proper module that uses Dependency Injection instead of the ObjectManager...

That said, there is some basic code I got working which renders the Simple Product variation image of a Configurable product, if you have that option enabled in the Minicart.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = null;

// Look up the simple product SKU
if ($sku = $_item->getProductOptionByCode('simple_sku')) {
    $productRepositoryInterface = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
    // get the simple product
    try {
        $product = $productRepositoryInterface->get($sku);
    } catch (Exception $e) {
        // this throws an exception if not found, so catch it
        if ($e->getMessage() != 'Requested product doesn\'t exist') {
            throw $e;
        }
    }
}

// if no simple product, get the usual product ID
if (!$product) {
    $product_id = $_item->getProductId();
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
}

// now load the image for that product
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$image_url = $_imagehelper->init($product, 'cart_page_product_thumbnail')->getUrl();
  • iam getting only one category image – shivashankar m May 12 '18 at 13:09

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.