0

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 - enter image description here

And what I would like to get is like this - enter image description here

Thank you a lot!

1

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.

  • It works, but is it possible to do this without using objectmanager? Thank you a lot! :) – help Dec 14 '18 at 12:30
1

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>
  • hi @prince patel, can you look into this issue please, magento.stackexchange.com/questions/253672/… – jafar pinjar Dec 14 '18 at 14:43
  • @PrincePatel Hey, thank you a lot for your help, it works, but question - how can I call it not inside review.phtml file but inside list.phtml file? Because if I call it inside review.phtml file it works, but if I call it inside list.phtml file it says "unknown" – help Dec 18 '18 at 12:00
  • @help for list.phtml you need to override class Magento\Catalog\Block\Product\ListProduct – Prince Patel Dec 18 '18 at 13:24
  • @PrincePatel Can you maybe please update your answer and show how to override that class? I keep getting unknown. P.S. I need to display image in 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
  • @help This question is about show product image on the review tab. Please ask another question for show image on the list page to keep clean and clear comments on this queue :) – Prince Patel Dec 19 '18 at 4:47

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.