The most interesting thing is that each child item is an instance of product model. So, we can re-use init()
method in Magento\Catalog\Helper\Image
. You try this code below:
<?php
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$productImage = $_imagehelper->init($_item, 'product_base_image')
->constrainOnly(FALSE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)->resize(50)->getUrl();
//echo $productImage;
These code lines above allow us to resize the item image and keep its Aspect Ratio.
product_base_image
is a global id of type image of Magento Catalog module. It may define in blank
,luma
or your theme. For example: vendor/magento/theme-frontend-luma/etc/view.xml
and vendor/magento/theme-frontend-blank/etc/view.xml
Or there is another way
$productImage = $block->getImage($_item, 'product_base_image')->getImageUrl();
//echo $productImage;
This is because the current block extends from Magento\Catalog\Block\Product\AbstractProduct
, so that, we can use exist method getImage()
.