TransWikia.com

Wrong product image in sales order email in production mode - Magento2.3.5

Magento Asked by JGeer on December 29, 2020

We use the following code to load the product image in the sales order email.
This works perfect in developer mode, but when we switch to production mode the images are not correct for those who do not got any image.

In that case the product that does not have any image, not the placeholder is shown as you should expect, but the same productimage as the product above is displayed.

The next product in line that does have any image, that is then displayed well.

See:

enter image description here

How can we solve this?

Template: /app/design/frontend/theme/theme/Magento_Sales/templates/email/items/invoice/default.phtml

CODE:

<?php $_item = $block->getItem()
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product_id = $_item->getProductId();
$product = $objectManager->get('MagentoCatalogModelProduct')->load($product_id);
$_imagehelper = $objectManager->get('MagentoCatalogHelperImage');
$image_url = $_imagehelper->init($product, 'cart_page_product_thumbnail')->getUrl();?>

<img src="<?= $image_url;?>"

One Answer

you may use a helper like below:

<?php

namespace MbsOrderEmailHelper;

use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppHelperContext;

class GetItemImage extends AbstractHelper
{
    /**
     * @var MagentoCatalogBlockProductImageBuilder
     */
    private $imageBuilder;

    public function __construct(
        Context $context,
        MagentoCatalogBlockProductImageBuilder $imageBuilder
    ) {
        parent::__construct($context);
        $this->imageBuilder = $imageBuilder;
    }

    public function getCartItemImage($item)
    {
        if ($item->getProduct() instanceof MagentoCatalogModelProduct) {
            return $this->imageBuilder->create($item->getProduct(), 'category_page_grid')->toHtml();
        }

        return '';
    }
}

then this helper will need to be used in your email template: the line below will output an image: the default email template for item in Magento is vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml. You may have customised this template and therefore you will need to fetch it within your theme folder.

<?= $helper->getCartItemImage($_item) ?>

Correct answer by Herve Tribouilloy on December 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP