I would like to include item images in my email confirmation for customer and internal reference. I added the code below that I found online as a solution to app/design/frontend/[VENDOR]/[TEMPLATE]/Magento_Sales/templates/email/items/order/default.phtml, but when the email comes in, it shows the parent (configurable) product image and not the child (simple) product image that I need.
$_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';
<img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('65','65')->getUrl();?>" alt="<?= __('Product Image');?>">
Does anyone know how to modify the email template to display the child (simple) product image and not the parent (configurable) product?
Hi @JPowell,
Here I share the code you can try this one. You can refer below
<?php if($_product->getTypeId() == "configurable"){
$_configurable = $_product->getTypeInstance()->getUsedProductIds();
foreach ($_configurable as $_config){
$_simpleproduct = Mage::getModel('catalog/product')->load($_config);
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_simpleproduct);
if($stock->getQty() && $stock->getIs_in_stock()){
foreach ($_simpleproduct->getMediaGalleryImages() as $image) {
echo '<img src="'.$image->getUrl().'" /><br>';
}
break;
}
}} ?>
If my solution is useful, give kudos & accept as solution
Best regards
Madhuresan
Bootsgrid
Unfortunately that solution did not work. I got an email stating "Error filtering template: Notice: Undefined variable: _product"
Hi @JPowell,
Once for clarification, did you customize the code corresponds to your needs? Or otherwise can I see your code what you have written?
If it is helpful, give kudos & accept as solution
Best regards
Madhuresan
bootsgrid
I just inserted the code you provided into defaults.phtml under the following line:
<p class="sku"><?= /* @escapeNotVerified */ __('SKU') ?>: <?= $block->escapeHtml($block->getSku($_item)) ?></p>
I am not familiar with php so I don't know how to tailor it.
The code I am trying to alter is /vendor/magento/module-sales/view/frontend/templates/email/items/order/defaults.phtml