0

In tab review, we display all reviews available in our website.

how to get product name assigned to a given review? https://prnt.sc/rhodfq because each review relates to different products and I would like to display the product name with a link to product redirection like here: https://prnt.sc/rhoeu0

3
  • hi, where you need it? Mar 17, 2020 at 10:38
  • please check my answer Mar 17, 2020 at 10:48
  • Please ask a question in detail, so it is helpful for others in the future Mar 17, 2020 at 10:51

2 Answers 2

1

Please try below code:

replace code on file

vendor/magento/module-review/view/frontend/templates/product/view/list.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var Magento\Review\Block\Product\View\ListView $block */

$_items = $block->getReviewsCollection()->getItems();
$format = $block->getDateFormat() ?: \IntlDateFormatter::SHORT;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$appState = $objectManager->get('\Magento\Framework\App\State');

$registry = $objectManager->get('\Magento\Framework\Registry');
$currentProduct = $registry->registry('current_product');
$product_name = $currentProduct->getName();
$product_url = $currentProduct->getProductUrl();


?>
<?php if (count($_items)):?>
<div class="block review-list" id="customer-reviews">
    <div class="block-title">
        <strong><?= $block->escapeHtml(__('Customer Reviews')) ?></strong>
    </div>
    <div class="block-content">
        <div class="toolbar review-toolbar">
            <?= $block->getChildHtml('toolbar') ?>
        </div>
        <ol class="items review-items">
        <?php foreach ($_items as $_review):?>
            <li class="item review-item" itemscope itemprop="review" itemtype="http://schema.org/Review">
                <p><a href="<?php echo $product_url; ?>"><?php echo $product_name; ?></a></p>
                <div class="review-title" itemprop="name"><?= $block->escapeHtml($_review->getTitle()) ?></div>
                <?php if (count($_review->getRatingVotes())): ?>
                    <div class="review-ratings">
                    <?php foreach ($_review->getRatingVotes() as $_vote): ?>
                    <div class="rating-summary item" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
                        <span class="label rating-label"><span><?= $block->escapeHtml($_vote->getRatingCode()) ?></span></span>
                        <div class="rating-result" title="<?= $block->escapeHtmlAttr($_vote->getPercent()) ?>%">
                            <meta itemprop="worstRating" content = "1"/>
                            <meta itemprop="bestRating" content = "100"/>
                            <span style="width:<?= $block->escapeHtmlAttr($_vote->getPercent()) ?>%">
                                <span itemprop="ratingValue"><?= $block->escapeHtml($_vote->getPercent()) ?>%</span>
                            </span>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    </div>
                <?php endif; ?>
                <div class="review-content" itemprop="description">
                    <?= nl2br($block->escapeHtml($_review->getDetail())) ?>
                </div>
                <div class="review-details">
                    <p class="review-author">
                        <span class="review-details-label"><?= $block->escapeHtml(__('Review by')) ?></span>
                        <strong class="review-details-value" itemprop="author"><?= $block->escapeHtml($_review->getNickname()) ?></strong>
                    </p>
                    <p class="review-date">
                        <span class="review-details-label"><?= $block->escapeHtml(__('Posted on')) ?></span>
                        <time class="review-details-value" itemprop="datePublished" datetime="<?= $block->escapeHtmlAttr($block->formatDate($_review->getCreatedAt(), $format)) ?>"><?= $block->escapeHtml($block->formatDate($_review->getCreatedAt(), $format)) ?></time>
                    </p>
                </div>
            </li>
        <?php endforeach; ?>
        </ol>
        <div class="toolbar review-toolbar">
            <?= $block->getChildHtml('toolbar') ?>
        </div>
    </div>
</div>
<?php endif;?>

Or for different product per review

<?php
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create("Magento\Catalog\Model\Product")->load($_review->getEntityPkValue()); 
     ?>
     <a href="<?= $product->getProductUrl() ?>"><?= $product->getName() ?></a>

Hope it works!

8
  • Please open our produkt card and click on tab "Opinie". gerris.pl/…
    – Sylvester
    Mar 17, 2020 at 11:01
  • ok wait let me check Mar 17, 2020 at 11:02
  • In this TAB we display all reviews from all products. We need: How to display product name above each review (because each review is for a different product) prnt.sc/rhodfq
    – Sylvester
    Mar 17, 2020 at 11:02
  • okay got it for this I need FTP access or theme files. Mar 17, 2020 at 11:04
  • or I can please let me know you added theme for that or extension? Mar 17, 2020 at 11:05
0

Override the File:

Magento/vendor/magento/module-review/view/frontend/templates/product/view/list.phtml

find the <?php foreach ($_items as $_review) :?> after that add

     <?php
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create("Magento\Catalog\Model\Product")->load($_review->getEntityPkValue()); 
     ?>
     <a href="<?= $product->getProductUrl() ?>"><?= $product->getName() ?></a>

Please don't use objectManager, create a block instead. I am just providing you a logic.

1
  • You are also right bro. Mar 18, 2020 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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