Gambar Produk dalam templat email faktur

11

Saya mencoba mendapatkan gambar produk untuk templat email faktur. Saya menggunakan kode di bawah ini. Tapi saya hanya mendapatkan gambar placeholder Magento di templat email.

<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>
Priya
sumber
Anda tidak dapat menggunakan PHP dalam templat email
Philipp Sander
saya tidak menggunakan dalam template email saya secara langsung, saya menambahkan kode ini dalam file saya "Magento_Sales / templates / email / items / shipment / default.phtml"
Priya
Apakah maksud Anda email pesanan?
Dava Gordon
Coba jawaban di bawah ini dan tetap poskan phtml lengkap Anda
Prathap Gunasekaran

Jawaban:

3

Saya menemukan solusinya tetapi mendapatkan gambar thumbnail induk, saya ingin mendapatkan jika produk telah memilih dalam opsi carikan, bahwa opsi carikan perlu ditampilkan.

contoh: jika saya memilih warna merah, gambar swatch warna merah perlu ditampilkan.


$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(); ?>" />
Priya
sumber
3

Saya telah menimpa 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;
            }
        }
    }
}

Yang mengembalikan saya PDF seperti ini. (bekerja pada rotasi Gambar :))

masukkan deskripsi gambar di sini

DIPERBARUI

Untuk produk berbasis varian - berdasarkan pada konfigurasi pemilihan atribut produk

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;
            }
        }
    }
}

Berikut gambar produk sederhana dalam Faktur berdasarkan pemilihan varian produk konfigurasi.

masukkan deskripsi gambar di sini

Referensi Lebih

Referensi 1 , Referensi 2 , Referensi 3

Aditya Shah
sumber
0

Anda dapat mengganti baris berikut dalam kode Anda

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

Dengan mengikuti garis

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

Dengan ini, Anda bisa mendapatkan produk sederhana dari produk yang dapat dikonfigurasi.

Yash Shah
sumber
0

Saya pikir Anda harus mencoba dengan cart_page_product_thumbnailalih - alih product_thumbnail_imagemendapatkan kode gambar produk Anda.

Kode Anda harus seperti ini.

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

Saya telah menggunakan kode di atas untuk menampilkan gambar produk dalam templat Email dan berfungsi baik dengan produk yang dapat dikonfigurasi. dan saya pikir itu juga berfungsi untuk templat email faktur.

Saya juga melihat begitu banyak pengguna yang menggunakan, cart_page_product_thumbnailsilakan periksa tautan referensi di bawah.

Saya harap ini membantu!

Chirag Patel
sumber