I have a page that shows a listing of entries (called products) by category. Those product entries have related content (reviews) stored in another channel that are tagged using a relations field.
On the category page, I want to show the overall rating (the sum of all ratings)for each individual product shown.
I'm calculating and showing the overall rating currently on the product profile page with no problems. But on the category page, I'm having trouble showing that overall rating also. I think my problem is where I'm using the relatedto field (in my queryParams variable) to associate the reviews to the product entry associated with the category.
Any help is appreciated.
<!-- get category from URL -->
{% set category = craft.categories.group('productCategories').slug(craft.request.lastSegment).one %}
<!-- display craft product entries related to category-->
{% set entries = craft.entries.relatedTo(category)
.limit(30)
.orderBy ('dateCreated desc')
.all() %}
<!-- set variable to grab the associated reviews -->
{% set queryParams = {
section: 'userProductReviews',
relatedTo: 'entry'
} %}
{% set reviewQuery = craft.entries(queryParams).all() %}
<!-- overall rating code -->
{# set sum of product ratings per entry#}
{% set sumOverallratings = 0 %}
{% for entry in reviewQuery %}
{% set sumOverallratings = sumOverallratings + entry.overallRating %}
{% endfor %}
{# the actual number of reviews related to product entry #}
{% set reviewCount = craft.entries(queryParams).total() %}
{# set the average for the rating #}
{% set productOverallAverage = sumOverallratings / reviewCount %}