0

I am using Magento 2.3.2 with custom theme. I want to add 'Review this product' in the Product listing view when there are no reviews. In the file

app/design/frontend/(vendor)/(theme-name)/Magento_Catalog/templates/product/list.phtml

I have tried adding this:

<?php if( count( $this->getReviewsCollection() ) > 0  ): ?>
<?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
<?php else: ?>
<div class="jag-reviews-text"><a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" class="product-review">Review this product</a></div>
<?php endif; ?>

But I get this error:this returns:

Exception #0 (Exception): Warning: count(): Parameter must be an array or an object that implements Countable in /home/iperqer/public_html/app/design/frontend/iperQ/market/Magento_Catalog/templates/product/list.phtml on line 72 plus a bunch of other code.

Any Ideas about how to achieve this.

0

PHP 7.2 throws error if parameter passed to the count function is empty. To get rid of this problem, you can change:

count($this->getReviewsCollection()) > 0

to

is_array($this->getReviewsCollection()) && count($this->getReviewsCollection()) > 0

This should solve the problem you are having.

Update after @jonnie's comment on Oct 10, 2019:

Instead of:

$this->getReviewsCollection()

try:

$this->getReviewsCollection()->getItems()
  • Thank you. This fixes the error thank you but I guess ` is_array($this->getReviewsCollection()) && count($this->getReviewsCollection()) > 0 ` is not pickup up the review count because all the listings now return "Review this product". Any ideas as to the fix. I read somewhere that I should be looking at the file summary_short.phtml. – Jonnie Oct 9 at 19:30
  • I also tried this <?php if( is_array($this->getReviewsCount()) && count($this->getReviewsCount()) > 0 ): ?> <?= $block->getReviewsSummaryHtml($_product, $templateType) ?> <?php else: ?> <div class="jag-reviews-text"><a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" class="product-review">Review this product</a></div> <?php endif; ?> with the same results. ( all the listings now return "Review this product" ) – Jonnie Oct 9 at 19:41

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.