I would like to get product image in reviews, can anyone help with some advice on how to do it?
Currently I have magento reviews -

And what I would like to get is like this -

Thank you a lot!
I would like to get product image in reviews, can anyone help with some advice on how to do it?
Currently I have magento reviews -

And what I would like to get is like this -

Thank you a lot!
In your custom theme create directory as
Magento_Review/templates/review.phtml
In review.phtml file write following code
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$helperImport = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageUrl = $helperImport->init($product, 'product_page_image_small')
->setImageFile($product->getSmallImage()) // image,small_image,thumbnail
->resize(380)
->getUrl();
<img src="<?php echo $imageUrl ?>" />
I hope this will help you.
Don't use ObjectManager directly in phtml files Learn More
1) Override class Magento\Review\Block\Product\Review
app/code/Vendor/Module/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Review\Block\Product\Review" type="Vendor\Module\Block\Product\Override\Review" />
</config>
2) Create Review.php
app/code/Vendor/Module/Block/Product/Override/Review.php
<?php
namespace Vendor\Module\Block\Product\Override;
class Review extends \Magento\Review\Block\Product\Review
{
protected $imageHelperFactory;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory,
\Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
array $data = []
) {
$this->imageHelperFactory = $imageHelperFactory;
parent::__construct($context, $registry, $collectionFactory, $data);
}
public function getProductImage()
{
$product = $this->_coreRegistry->registry('product');
$imageUrl = $this->imageHelperFactory->create()
->init($product, 'product_base_image')->getUrl();
return $imageUrl;
}
}
3) Now in review.phtml call getProductImage() function to get image url
app/design/frontend/Vendor/Theme/Magento_Review/templates/review.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Review\Block\Product\Review $block */
?>
<div>
<img src="<?= $block->getProductImage() ?>" />
</div>
<div id="product-review-container" data-role="product-review"></div>
<?= $block->getChildHtml() ?>
<script type="text/x-magento-init">
{
"*": {
"Magento_Review/js/process-reviews": {
"productReviewUrl": "<?= $block->escapeJs($block->escapeUrl($block->getProductReviewUrl())) ?>",
"reviewsTabSelector": "#tab-label-reviews"
}
}
}
</script>
list.phtml you need to override class Magento\Catalog\Block\Product\ListProduct
– Prince Patel
Dec 18 '18 at 13:24
module-review/frontend/templates/product/view/list.phtml, is it really the right file you are saying that I need to override?
– help
Dec 18 '18 at 14:15
at Magento_Review\templates\form.phtml add this code :
<?php
foreach ($block->getProductInfo()->getMediaGallery('images') as $image) {
$imageUrl = $block->getProductInfo()->getMediaConfig()->getMediaUrl($image['file']);
}
?>
and then you can show the image like this :
<img class="product-image" src="<?= $imageUrl ?>" />
Magento ver. 2.3.1