11

I am trying to get product images for invoice email template. I used below code. But I am only getting Magento placeholder image in email template.

<td>
    <?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product_id = $_item->getOrderItem()->getProduct();
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

    $_imagehelper = $objectManager->get('Magento\Catalog\Helper\Image');
    $image_url = $_imagehelper->init($product, 'cart_page_product_thumbnail')->getUrl();

    ?>
      <img src="<?php echo $image_url; ?>" alt="<?php echo $product->getName(); ?>" />
</td>
  • you can't use PHP in email templates – Philipp Sander Mar 21 at 7:13
  • i did not use in my email template directly, i added this code in my file "Magento_Sales/templates/email/items/shipment/default.phtml" – mohana Mar 21 at 7:54
  • Do you mean the order email? – Dava Gordon Apr 12 at 10:57
  • Try the below answer and keep post your complete phtml – Prathap Gunasekaran Apr 13 at 3:24
3

I found the solution but it's getting parent thumbnail image, i like to get if product has selected in swatch option, that swatch option need to display.

example: if i select red colour, red colour swatch image need to display.


$productId = $_item->getProductId();
$objectManagerHere = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManagerHere->get('Magento\Catalog\Model\Product')->load($productId);

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper  = $_objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->resize(80, 80)->getUrl();

<img src="<?php echo $image_url; ?>" alt="<?php echo $product->getName(); ?>" />
3
+50

I have override DefaultInvoice

class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice
{
    public function draw()
    {
        $order = $this->getOrder();
        $item = $this->getItem();
        $pdf = $this->getPdf();
        $page = $this->getPage();
        $lines = [];


        // draw Product image
        $productImage = $this->getProductImage($item, $page);

        // draw Product name
        $lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];

        $lines[0][] = array(
            'text'  => $productImage,
            'is_image'  => 1,
            'feed'  => 200
        );

        // draw SKU
        $lines[0][] = [
            'text' => $this->string->split($this->getSku($item), 17),
            'feed' => 370,
            'align' => 'right',
        ];

        // draw QTY
        $lines[0][] = ['text' => $item->getQty() * 1, 'feed' => 475, 'align' => 'right'];

        // draw item Prices
        $i = 0;
        $prices = $this->getItemPricesForDisplay();
        $feedPrice = 425;
        $feedSubtotal = $feedPrice + 140;
        foreach ($prices as $priceData) {
            if (isset($priceData['label'])) {
                // draw Price label
                $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
                // draw Subtotal label
                $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
                $i++;
            }
            // draw Price
            $lines[$i][] = [
                'text' => $priceData['price'],
                'feed' => $feedPrice,
                'font' => 'bold',
                'align' => 'right',
            ];
            // draw Subtotal
            $lines[$i][] = [
                'text' => $priceData['subtotal'],
                'feed' => $feedSubtotal,
                'font' => 'bold',
                'align' => 'right',
            ];
            $i++;
        }

        // draw Tax
        $lines[0][] = [
            'text' => $order->formatPriceTxt($item->getTaxAmount()),
            'feed' => 515,
            'font' => 'bold',
            'align' => 'right',
        ];

        // custom options
        $options = $this->getItemOptions();
        if ($options) {
            foreach ($options as $option) {
                // draw options label
                $lines[][] = [
                    'text' => $this->string->split($this->filterManager->stripTags($option['label']), 40, true, true),
                    'font' => 'italic',
                    'feed' => 35,
                ];

                if ($option['value']) {
                    if (isset($option['print_value'])) {
                        $printValue = $option['print_value'];
                    } else {
                        $printValue = $this->filterManager->stripTags($option['value']);
                    }
                    $values = explode(', ', $printValue);
                    foreach ($values as $value) {
                        $lines[][] = ['text' => $this->string->split($value, 30, true, true), 'feed' => 40];
                    }
                }
            }
        }

        $lineBlock = ['lines' => $lines, 'height' => 20];

        $page = $pdf->drawLineBlocks($page, [$lineBlock], ['table_header' => true],1);

        $this->setPage($page);
    }


    /*
     * Return Value of custom attribute
     * */
    private function getProductImage($item,  &$page)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $productId = $item->getOrderItem()->getProductId();
        $image = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);

        if (!is_null($image)) {
            try{

                $imagePath = '/catalog/product/'.$image->getSmallImage();

                $filesystem = $objectManager->get('Magento\Framework\Filesystem');
                $media_dir = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);

                if ($media_dir->isFile($imagePath)) {
                    return $media_dir->getAbsolutePath($imagePath);
                }
                else
                    return null;
            }
            catch (Exception $e) {
                return false;
            }
        }
    }
}

Which returns me PDF like this. (working on Image rotation :) )

enter image description here

UPDATED

For variant based product - based on configure product's attribute selection

class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice
{
    public function draw()
    {
        $order = $this->getOrder();
        $item = $this->getItem();
        $pdf = $this->getPdf();
        $page = $this->getPage();
        $lines = [];


        // draw Product image
        $productImage = $this->getProductImage($item, $page);

        // draw Product name
        $lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];

        $lines[0][] = array(
            'text'  => $productImage,
            'is_image'  => 1,
            'feed'  => 200
        );

        // draw SKU
        $lines[0][] = [
            'text' => $this->string->split($this->getSku($item), 17),
            'feed' => 370,
            'align' => 'right',
        ];

        // draw QTY
        $lines[0][] = ['text' => $item->getQty() * 1, 'feed' => 475, 'align' => 'right'];

        // draw item Prices
        $i = 0;
        $prices = $this->getItemPricesForDisplay();
        $feedPrice = 425;
        $feedSubtotal = $feedPrice + 140;
        foreach ($prices as $priceData) {
            if (isset($priceData['label'])) {
                // draw Price label
                $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
                // draw Subtotal label
                $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
                $i++;
            }
            // draw Price
            $lines[$i][] = [
                'text' => $priceData['price'],
                'feed' => $feedPrice,
                'font' => 'bold',
                'align' => 'right',
            ];
            // draw Subtotal
            $lines[$i][] = [
                'text' => $priceData['subtotal'],
                'feed' => $feedSubtotal,
                'font' => 'bold',
                'align' => 'right',
            ];
            $i++;
        }

        // draw Tax
        $lines[0][] = [
            'text' => $order->formatPriceTxt($item->getTaxAmount()),
            'feed' => 515,
            'font' => 'bold',
            'align' => 'right',
        ];

        // custom options
        $options = $this->getItemOptions();
        if ($options) {
            foreach ($options as $option) {
                // draw options label
                $lines[][] = [
                    'text' => $this->string->split($this->filterManager->stripTags($option['label']), 40, true, true),
                    'font' => 'italic',
                    'feed' => 35,
                ];

                if ($option['value']) {
                    if (isset($option['print_value'])) {
                        $printValue = $option['print_value'];
                    } else {
                        $printValue = $this->filterManager->stripTags($option['value']);
                    }
                    $values = explode(', ', $printValue);
                    foreach ($values as $value) {
                        $lines[][] = ['text' => $this->string->split($value, 30, true, true), 'feed' => 40];
                    }
                }
            }
        }

        $lineBlock = ['lines' => $lines, 'height' => 20];

        $page = $pdf->drawLineBlocks($page, [$lineBlock], ['table_header' => true],1);

        $this->setPage($page);
    }


    /*
     * Return Value of custom attribute
     * */
    private function getProductImage($item,  &$page)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $productId = $item->getOrderItem()->getProductId();
        $data = $objectManager->get('Magento\Catalog\Model\ProductRepository')->get($item->getSku());
        $image = $data->getImage();

        if (!is_null($image)) {
            try{

                $imagePath = '/catalog/product/'.$image;

                $filesystem = $objectManager->get('Magento\Framework\Filesystem');
                $media_dir = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);

                if ($media_dir->isFile($imagePath)) {
                    return $media_dir->getAbsolutePath($imagePath);
                }
                else
                    return null;
            }
            catch (Exception $e) {
                return false;
            }
        }
    }
}

Here's simple product's image in Invoice based of variant selection of configure product.

enter image description here

More References

Reference 1 , Reference 2 , Reference 3

0

You can replace following line in your code

$product = $objectManagerHere->get('Magento\Catalog\Model\Product')->load($productId);

With following line

$product = $objectManagerHere->get('Magento\Catalog\Model\ProductRepository')->get($_item->getSku());

With this, you can get appropriate simple product of configurable product.

0

I think you should try with cart_page_product_thumbnail instead of product_thumbnail_image in your get product image code.

Your code should be like this.

$image_url = $imageHelper->init($product, 'cart_page_product_thumbnail')->setImageFile($product->getFile())->resize(80, 80)->getUrl();

I have used the above code to display product image in the Email template and it is working fine with configurable products. and I think it also works for the invoice email template.

I have also seen so many users use cart_page_product_thumbnail please check below reference link.

I hope it helps!

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.