0

Currently, on the list of products starts is displayed only when someone adds reviews. I always needs to display the stars even when there is also no rating just like on the home page.

Here is screen from home page:

enter image description here

I want also display on products listing: enter image description here

here is code summary.phtml:

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

// @codingStandardsIgnoreFile

/** @var \Magento\Review\Block\Product\ReviewRenderer $block */

$url = $block->getReviewsUrl() . '#reviews';
$urlForm = $block->getReviewsUrl() . '#review-form';
?>
<?php if ($block->getReviewsCount()): ?>
<?php $rating = $block->getRatingSummary(); ?>
<div class="product-reviews-summary<?= !$rating ? ' no-rating' : '' ?>" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
    <?php if ($rating):?>
    <div class="rating-summary">
         <span class="label"><span><?= $block->escapeHtml(__('Rating')) ?>:</span></span>
         <div class="rating-result" title="<?= $block->escapeHtmlAttr($rating); ?>%">
             <span style="width:<?= $block->escapeHtmlAttr($rating); ?>%">
                 <span>
                     <span itemprop="ratingValue"><?= $block->escapeHtml($rating); ?></span>% of <span itemprop="bestRating">100</span>
                 </span>
             </span>
         </div>
     </div>
    <?php endif;?>
    <div class="reviews-actions">
        <a class="action view"
           href="<?= $block->escapeUrl($url) ?>">
            <span itemprop="reviewCount"><?= $block->escapeHtml($block->getReviewsCount()) ?></span>&nbsp;
            <span><?= ($block->getReviewsCount() == 1) ? $block->escapeHtml(__('Review')) : $block->escapeHtml(__('Reviews')) ?></span>
        </a>
        <a class="action add" href="<?= $block->escapeUrl($urlForm) ?>"><?= $block->escapeHtml(__('Add Your Review')) ?></a>
    </div>
</div>
<?php elseif ($block->getDisplayIfEmpty()): ?>
<div class="product-reviews-summary empty">
    <div class="reviews-actions">
        <a class="action add" href="<?= $block->escapeUrl($urlForm) ?>">
            <?= $block->escapeHtml(__('Be the first to review this product')) ?>
        </a>
    </div>
</div>
<?php endif; ?>

list.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\App\Action\Action;

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Product list template
 *
 * @var $block \Magento\Catalog\Block\Product\ListProduct
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper            = $this->helper('Magento\Catalog\Helper\Output');
$_config            = $this->helper('Sm\Market\Helper\Data');
$homeStyle          = $_config->getThemeLayout('home_style');
?>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php /* @escapeNotVerified */
            echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <div class="toolbar-top">
    <?php echo $block->getToolbarHtml() ?>
    </div>
    <?php echo $block->getAdditionalHtml() ?>
    <?php
    if ($block->getMode() == 'grid') {
        $viewMode        = 'grid';
        $image           = 'category_page_grid';
        $showDescription = false;
        $templateType    = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    } else {
        $viewMode        = 'list';
        $image           = 'category_page_list';
        $showDescription = true;
        $templateType    = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
    }
    /**
     * Position for actions regarding image size changing in vde if needed
     */
    $pos = $block->getPositioned();
    ?>
    <div class="category-product products wrapper <?php /* @escapeNotVerified */
    echo $viewMode; ?> products-<?php /* @escapeNotVerified */
    echo $viewMode; ?>">
        <?php $iterator = 1; ?>

        <?php if ($viewMode == 'grid') { ?>

            <ol class="products list items product-items row">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
                <?php foreach ($_productCollection as $_product): ?>
                    <li class="item product product-item">
                    <div class="product-item-info" data-container="product-grid">
                        <div class="item-inner">
                            <div class="box-image">
                                <?php $productImage = $block->getImage($_product, $image); ?>

                                <!--LABEL PRODUCT-->

                                <?php
                                $orgprice             = $_product->getPrice();
                                $specialprice         = $_product->getSpecialPrice();
                                $specialPriceFromDate = $_product->getSpecialFromDate();
                                $specialPriceToDate   = $_product->getSpecialToDate();

                                $today = time();

                                if ($specialprice && $_config->getAdvanced('show_salelabel')) {
                                    if ($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) { ?>
                                        <?php if ($specialprice < $orgprice) {
                                            $save_percent = 100 - round(($specialprice / $orgprice) * 100);
                                            ?>
                                            <div class="label-product label-sale">
                                            <span class="sale-product-icon">
                                                <?php echo '-' . $save_percent . '%'; ?>
                                            </span>
                                        </div>
                                        <?php } ?>
                                    <?php }
                                }
                                ?>

                                <?php
                                $now      = date("Y-m-d");
                                $newsFrom = substr($_product->getNewsFromDate(), 0, 10);
                                $newsTo   = substr($_product->getNewsToDate(), 0, 10);


                                if (($newsTo != '' || $newsFrom != '') && $_config->getAdvanced('show_newlabel')) {
                                    if (($newsTo != '' && $newsFrom != '' && $now >= $newsFrom && $now <= $newsTo) || ($newsTo == '' && $now >= $newsFrom) || ($newsFrom == '' && $now <= $newsTo)) { ?>
                                        <div class="label-product label-new">
                                                <span class="new-product-icon"><?php echo __('New'); ?></span>
                                            </div>
                                    <?php }
                                } ?>


                                <!--END LABEL PRODUCT-->

                                <a href="<?php /* @escapeNotVerified */
                                echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                                        <?php echo $productImage->toHtml(); ?>
                                </a>

                                <?php if ($homeStyle == 'home-16' || $homeStyle == 'home-17' || $homeStyle == 'home-18' || $homeStyle == 'home-19' || $homeStyle == 'home-20') { ?>
                                    <div class="button-action">
                                        <?php if ($_config->getAdvanced('show_wishlist_button')) { ?>
                                            <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                                <a href="#" class="action towishlist btn-action link-wishlist" title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" data-post='<?php /* @escapeNotVerified */
                                                echo $block->getAddToWishlistParams($_product); ?>' data-action="add-to-wishlist" role="button">
                                                    <span><?php /* @escapeNotVerified */
                                                        echo __('Add to Wish List') ?></span>
                                                </a>
                                            <?php endif; ?>
                                        <?php } ?>

                                        <?php if ($_config->getAdvanced('show_compare_button')) { ?>
                                            <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                            <a href="#" class="action tocompare btn-action link-compare" title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>" data-post='<?php /* @escapeNotVerified */
                                            echo $compareHelper->getPostDataParams($_product); ?>' role="button">
                                                <span><?php /* @escapeNotVerified */
                                                    echo __('Add to Compare') ?></span>
                                            </a>
                                        <?php } ?>
                                    </div>
                                <?php } ?>
                            </div>

                            <div class="product details product-item-details box-info">
                                <?php
                                $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                                ?>
                                <h2 class="product name product-item-name product-name">
                                    <a class="product-item-link" href="<?php /* @escapeNotVerified */
                                    echo $_product->getProductUrl() ?>">
                                        <?php /* @escapeNotVerified */
                                        echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                                    </a>
                                </h2>

                                <?php echo $block->getReviewsSummaryHtml($_product, $templateType, true); ?>
                                <?php /* @escapeNotVerified */
                                echo $block->getProductPrice($_product) ?>
                                <?php echo $block->getProductDetailsHtml($_product); ?>

                                <?php if ($homeStyle == 'home-16' || $homeStyle == 'home-17' || $homeStyle == 'home-18' || $homeStyle == 'home-19' || $homeStyle == 'home-20') { ?>
                                    <div class="bottom-action">
                                        <?php if ($_config->getAdvanced('show_addtocart_button')) { ?>
                                            <?php if ($_product->isSaleable()): ?>
                                                <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                <form data-role="tocart-form" action="<?php /* @escapeNotVerified */
                                                echo $postParams['action']; ?>" method="post">
                                                    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */
                                                    echo $postParams['data']['product']; ?>">
                                                    <input type="hidden" name="<?php /* @escapeNotVerified */
                                                    echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */
                                                    echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                    <?php echo $block->getBlockHtml('formkey') ?>
                                                    <button type="submit"
                                                            title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                            class="action tocart primary btn-action btn-cart">
                                                        <span><?php /* @escapeNotVerified */
                                                            echo __('Add to Cart') ?></span>
                                                    </button>
                                                </form>
                                            <?php else: ?>
                                                <?php if ($_product->getIsSalable()): ?>
                                                    <div class="stock available"><span><?php /* @escapeNotVerified */
                                                            echo __('In stock') ?></span></div>
                                                <?php else: ?>
                                                    <div class="stock unavailable btn-action btn-cart"><span><?php /* @escapeNotVerified */
                                                            echo __('Out of stock') ?></span></div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        <?php } ?>
                                    </div>
                                <?php } ?>

                                <?php if ($homeStyle != 'home-16' && $homeStyle != 'home-17' && $homeStyle != 'home-18' && $homeStyle != 'home-19' && $homeStyle != 'home-20') { ?>
                                    <div class="bottom-action">
                                        <?php if ($_config->getAdvanced('show_wishlist_button')) { ?>
                                            <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                                <a href="#" class="action towishlist btn-action link-wishlist" title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" data-post='<?php /* @escapeNotVerified */
                                                echo $block->getAddToWishlistParams($_product); ?>' data-action="add-to-wishlist" role="button">
                                                    <span><?php /* @escapeNotVerified */
                                                        echo __('Add to Wish List') ?></span>
                                                </a>
                                            <?php endif; ?>
                                        <?php } ?>

                                        <?php if ($_config->getAdvanced('show_addtocart_button')) { ?>
                                            <?php if ($_product->isSaleable()): ?>
                                                <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                <form data-role="tocart-form" action="<?php /* @escapeNotVerified */
                                                echo $postParams['action']; ?>" method="post">
                                                    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */
                                                    echo $postParams['data']['product']; ?>">
                                                    <input type="hidden" name="<?php /* @escapeNotVerified */
                                                    echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */
                                                    echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                    <?php echo $block->getBlockHtml('formkey') ?>
                                                    <button type="submit"
                                                            title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                            class="action tocart primary btn-action btn-cart">
                                                        <span><?php /* @escapeNotVerified */
                                                            echo __('Add to Cart') ?></span>
                                                    </button>
                                                </form>
                                            <?php else: ?>
                                                <?php if ($_product->getIsSalable()): ?>
                                                    <div class="stock available"><span><?php /* @escapeNotVerified */
                                                            echo __('In stock') ?></span></div>
                                                <?php else: ?>
                                                    <div class="stock unavailable btn-action btn-cart"><span><?php /* @escapeNotVerified */
                                                            echo __('Out of stock') ?></span></div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        <?php } ?>

                                        <?php if ($_config->getAdvanced('show_compare_button')) { ?>
                                            <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                            <a href="#" class="action tocompare btn-action link-compare" title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>" data-post='<?php /* @escapeNotVerified */
                                            echo $compareHelper->getPostDataParams($_product); ?>' role="button">
                                                <span><?php /* @escapeNotVerified */
                                                    echo __('Add to Compare') ?></span>
                                            </a>
                                        <?php } ?>
                                    </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </li>
                <?php endforeach; ?>
        </ol>

        <?php } else { ?>
            <ol class="products list items product-items">
                <?php foreach ($_productCollection as $_product): ?>
                    <li class="item respl-item-list product product-item">
                        <div class="product-item-info" data-container="product-grid">
                            <div class="item-inner">
                                <div class="box-image-list">
                                    <?php $productImage = $block->getImage($_product, $image); ?>

                                    <!--LABEL PRODUCT-->

                                    <?php
                                    $specialprice         = $_product->getSpecialPrice();
                                    $specialPriceFromDate = $_product->getSpecialFromDate();
                                    $specialPriceToDate   = $_product->getSpecialToDate();

                                    $today = time();

                                    if ($specialprice && $_config->getAdvanced('show_salelabel')) {
                                        if ($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) { ?>
                                            <div class="label-product label-sale">
                                                        <span class="sale-product-icon">
                                                            <?php echo __('Sale'); ?>
                                                        </span>
                                                    </div>
                                        <?php }
                                    }
                                    ?>

                                    <?php
                                    $now      = date("Y-m-d");
                                    $newsFrom = substr($_product->getNewsFromDate(), 0, 10);
                                    $newsTo   = substr($_product->getNewsToDate(), 0, 10);


                                    if (($newsTo != '' || $newsFrom != '') && $_config->getAdvanced('show_newlabel')) {
                                        if (($newsTo != '' && $newsFrom != '' && $now >= $newsFrom && $now <= $newsTo) || ($newsTo == '' && $now >= $newsFrom) || ($newsFrom == '' && $now <= $newsTo)) { ?>
                                            <div class="label-product label-new">
                                                        <span class="new-product-icon"><?php echo __('New'); ?></span>
                                                    </div>
                                        <?php }
                                    } ?>


                                    <!--END LABEL PRODUCT-->

                                    <a href="<?php /* @escapeNotVerified */
                                    echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                                            <?php echo $productImage->toHtml(); ?>
                                    </a>

                                </div>

                                <div class="product details product-item-details box-info-list">
                                    <?php
                                    $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                                    ?>
                                    <h2 class="product name product-item-name product-name">
                                        <a class="product-item-link" href="<?php /* @escapeNotVerified */
                                        echo $_product->getProductUrl() ?>">
                                            <?php /* @escapeNotVerified */
                                            echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                                        </a>
                                    </h2>

                                    <?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                                    <?php /* @escapeNotVerified */
                                    echo $block->getProductPrice($_product) ?>
                                    <?php echo $block->getProductDetailsHtml($_product); ?>

                                    <div class="product-item-inner">
                                        <?php if ($showDescription): ?>
                                            <div class="product description product-item-description">
                                                <?php /* @escapeNotVerified */
                                                echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                                <a href="<?php /* @escapeNotVerified */
                                                echo $_product->getProductUrl() ?>" title="<?php /* @escapeNotVerified */
                                                echo $_productNameStripped ?>"
                                                   class="action more"><?php /* @escapeNotVerified */
                                                    echo __('Learn More') ?></a>
                                            </div>
                                        <?php endif; ?>
                                    </div>

                                    <div class="bottom-action">
                                        <?php if ($_config->getAdvanced('show_addtocart_button')) { ?>
                                            <?php if ($_product->isSaleable()): ?>
                                                <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                <form data-role="tocart-form" action="<?php /* @escapeNotVerified */
                                                echo $postParams['action']; ?>" method="post">
                                                    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */
                                                    echo $postParams['data']['product']; ?>">
                                                    <input type="hidden" name="<?php /* @escapeNotVerified */
                                                    echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */
                                                    echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                    <?php echo $block->getBlockHtml('formkey') ?>
                                                    <button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart btn-action btn-cart">
                                                        <span><?php /* @escapeNotVerified */
                                                            echo __('Add to Cart') ?></span>
                                                    </button>
                                                </form>
                                            <?php else: ?>
                                                <?php if ($_product->getIsSalable()): ?>
                                                    <div class="stock available"><span><?php /* @escapeNotVerified */
                                                            echo __('In stock') ?></span></div>
                                                <?php else: ?>
                                                    <div class="stock unavailable btn-action btn-cart"><span><?php /* @escapeNotVerified */
                                                            echo __('Out of stock') ?></span></div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        <?php } ?>

                                        <div class="add-to-links" data-role="add-to-links" class="actions-secondary">
                                            <?php if ($_config->getAdvanced('show_wishlist_button')) { ?>
                                                <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                                    <a href="#" class="action towishlist btn-action link-wishlist" title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" data-post='<?php /* @escapeNotVerified */
                                                    echo $block->getAddToWishlistParams($_product); ?>' data-action="add-to-wishlist" role="button">
                                                        <span><?php /* @escapeNotVerified */
                                                            echo __('Add to Wish List') ?></span>
                                                    </a>
                                                <?php endif; ?>
                                            <?php } ?>

                                            <?php if ($_config->getAdvanced('show_compare_button')) { ?>
                                                <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                                <a href="#" class="action tocompare btn-action link-compare" title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>" data-post='<?php /* @escapeNotVerified */
                                                echo $compareHelper->getPostDataParams($_product); ?>' role="button">
                                                    <span><?php /* @escapeNotVerified */
                                                        echo __('Add to Compare') ?></span>
                                                </a>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ol>
        <?php } ?>
    </div>
    <div class="toolbar-bottom">
    <?php echo $block->getToolbarHtml() ?>
    </div>
    <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
        {
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {}
            }
        }
        </script>
    <?php endif; ?>
<?php endif; ?>
1

copy the template summary_short.phtml from:

vendor/magento/module_review/view/frontend/templates/helper/

to

app/design/frontend/[vendor]/[theme]/Magento_Review/templates/helper

change line 29 from:

<?php elseif ($block->isReviewEnabled() && $block->getDisplayIfEmpty()): ?>

to

<?php elseif ($block->isReviewEnabled()): ?>

this will display the text: Be the first to review this product' as a link to the product reviews tab if the product has no reviews at all. It will not display the empty stars but it will display the appropriate count (0) and the correct link.

| improve this answer | |

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.