Magento – Magento 2.1.7 – Showing Rating on Category Page

magento-2.1ratings

I have been trying to show the rating on the category listing page. They show up if there is a rating, but I want rating to show even if there are no reviews (no stars).

In the list.phtml page i see:

<?php echo $block->getReviewsSummaryHtml($_product, $templateType, true); ?>

I also see the function

    public function aroundGetReviewsSummaryHtml(
    ListProduct $subject,
    callable $proceed,
    \Magento\Catalog\Model\Product $product,
    $templateType = false,
    $displayIfNoReviews = false)
{
    return $this->_helper->getCategoryShowReviews($subject->getMode())
        ? $proceed($product, $templateType, $displayIfNoReviews)
        : '';
}

I have tried a lot of things but can not get anything to show if there are no reviews. The end of the functions has a variable $displayIfNoReviews, but if i set this to true, still doesn't show.

Anyone have any ideas I can try?

EDIT 1:
I have changed the summary_short file with the code below. But its showing 0% now. Not the ratings.

    <div class="product-reviews-summary short empty">
    <?php $rating = 0 ;?>
    <div class="rating-summary">
        <span class="label"><span><?php /* @escapeNotVerified */ echo __('Rating') ?>:</span></span>
        <div class="rating-result" title="<?php /* @escapeNotVerified */ echo $rating; ?>%">
            <span style="width:<?php /* @escapeNotVerified */ echo $rating; ?>%"><span><?php /* @escapeNotVerified */ echo $rating; ?>%</span></span>
        </div>
    </div>
   
    <div class="reviews-actions">
        <a class="action add" href="<?php /* @escapeNotVerified */ echo $urlForm; ?>">
            <?php /* @escapeNotVerified */ echo __('Be the first to review this product') ?>
        </a>
    </div>
</div>

EDIT 2:
Percent

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

// @codingStandardsIgnoreFile

?>

<?php $url = $block->getReviewsUrl() . '#reviews'; ?>
<?php $urlForm = $block->getReviewsUrl() . '#review-form'; ?>
<?php if ($block->getReviewsCount()): ?>
    <?php $rating = $block->getRatingSummary(); ?>

       <div class="product-reviews-summary short<?php echo !$rating ? ' no-rating' : ''?>">
        <?php if ($rating):?>
            <div class="rating-summary">
                <span class="label"><span><?php /* @escapeNotVerified */ echo __('Rating') ?>:</span></span>

                <div class="rating-result__stars 111">
                    <?php
                    $half_star_id = round( (int)$rating / 20 );
                    for($i = 0; $i < 5; $i++){
                        if( $i < $half_star_id ) {
                            echo '<span class="rating-star__full"></span>';
                        }
                        if( $i == $half_star_id ) {
                            echo '<span class="rating-star__half"></span>';
                        }
                        if( $i > $half_star_id ) {
                            echo '<span class="rating-star__empty"></span>';
                        }
                    } ?>
                </div>

                <div class="rating-result__text">
                    <span style="width:<?php /* @escapeNotVerified */ echo $rating; ?>%"><span><?php /* @escapeNotVerified */ echo $rating; ?>%</span></span>
                </div>
            </div>
        <?php endif;?>
        
        <div class="reviews-actions">
            <a class="action view" href="<?php /* @escapeNotVerified */ echo $url ?>"><?php /* @escapeNotVerified */ echo $block->getReviewsCount() ?>&nbsp;<span><?php /* @escapeNotVerified */ echo($block->getReviewsCount() == 1) ? __('Review') : __('Reviews') ?></span></a>
        </div>
    </div>
<?php elseif ($block->getDisplayIfEmpty()): ?>

    <div class="product-reviews-summary short empty">
        <?php $rating = 0 ;?>
        <div class="rating-summary">
            <span class="label"><span><?php /* @escapeNotVerified */ echo __('Rating') ?>:</span></span>
            <div class="rating-result" title="<?php /* @escapeNotVerified */ echo $rating; ?>%">
                <span style="width:<?php /* @escapeNotVerified */ echo $rating; ?>%"><span><?php /* @escapeNotVerified */ echo $rating; ?>%</span></span>
            </div>
        </div>
       
        <div class="reviews-actions">
            <a class="action add" href="<?php /* @escapeNotVerified */ echo $urlForm; ?>">
                <?php /* @escapeNotVerified */ echo __('Be the first to review this product') ?>
            </a>
        </div>
    </div>
<?php else: ?>   
    <div class="testclass2"></div>
      <?php $rating = $block->getRatingSummary(); ?>
       
       <div class="product-reviews-summary short<?php echo !$rating ? ' no-rating' : ''?>">
        <?php if ($rating):?>
            <div class="rating-summary">
                <span class="label"><span><?php /* @escapeNotVerified */ echo __('Rating') ?>:</span></span>

                <div class="rating-result__stars 11">
                    <?php
                    $half_star_id = round( (int)$rating / 20 );
                    for($i = 0; $i < 5; $i++){
                        if( $i < $half_star_id ) {
                            echo '<span class="rating-star__full"></span>';
                        }
                        if( $i == $half_star_id ) {
                            echo '<span class="rating-star__half"></span>';
                        }
                        if( $i > $half_star_id ) {
                            echo '<span class="rating-star__empty"></span>';
                        }
                    } ?>
                </div>

                <div class="rating-result__text">
                    <span style="width:<?php /* @escapeNotVerified */ echo $rating; ?>%"><span><?php /* @escapeNotVerified */ echo $rating; ?>%</span></span>
                </div>
            </div>
        <?php endif;?>
        <div class="reviews-actions">
            <a class="action view" href="<?php /* @escapeNotVerified */ echo $url ?>"><?php /* @escapeNotVerified */ echo $block->getReviewsCount() ?>&nbsp;<span><?php /* @escapeNotVerified */ echo($block->getReviewsCount() == 1) ? __('Review') : __('Reviews') ?></span></a>
        </div>
    </div>
<?php endif; ?>

Best Answer

If you want to show rating on those products which does not have any rating then you need to some changes at template phtml files also there are no need to use aroundGetReviewsSummaryHtml. Also make <?php echo $block->getReviewsSummaryHtml($_product, $templateType, true); ?>

Related Topic