Skip to content

Instantly share code, notes, and snippets.

@freakdesign
Created April 11, 2016 15:53
Show Gist options
  • Save freakdesign/ba9e4bac8a6221651a33c2d94883812f to your computer and use it in GitHub Desktop.
Save freakdesign/ba9e4bac8a6221651a33c2d94883812f to your computer and use it in GitHub Desktop.
Show a collection based summary using data from the Shopify Product Reviews App.
{% comment %}
Usage:
{% include 'collection-ratings' %}
{% include 'collection-ratings' collectionHandle:'some-handle' useAll:true %}
{% endcomment %}
{% if collectionHandle == blank %}{% assign collectionHandle=collection.handle %}{% endif %}
{% if collections[collectionHandle].products.size > 0 %}
{% comment %} Setup the basic vars {% endcomment %}
{% assign collection = collections[collectionHandle] %}
{% assign reviewAverage = 0 %}
{% assign reviewVotes = 0 %}
{% assign reviewedProducts = 0 %}
{% assign maxReview = 5 %}
{% comment %} The two vars below is sections of the metafield we need to find {% endcomment %}
{% assign splitMarkerAverage = 'itemprop="average" content="' %}
{% assign splitMarkerVotes = 'itemprop="votes" content="' %}
{% comment %} Loop over the product {% endcomment %}
{% for product in collection.products limit:50 %}
{% comment %} Grab the review metafield {% endcomment %}
{% assign productReviewMetafield = product.metafields.spr.reviews %}
{% comment %} Some quick checks to see if the metafield exists, and has what we need. {% endcomment %}
{% if productReviewMetafield == blank %}{% continue %}{% endif %}
{% unless productReviewMetafield contains splitMarkerAverage and productReviewMetafield contains splitMarkerVotes %}{% continue %}{% endunless %}
{% comment %} Get the values from the metafield (hopefully)! {% endcomment %}
{% assign productAverage = productReviewMetafield | split:splitMarkerAverage | last | split:'"' | first | abs %}
{% assign productVotes = productReviewMetafield | split:splitMarkerVotes | last | split:'"' | first | abs %}
{% comment %} Update our values {% endcomment %}
{% assign reviewAverage = reviewAverage | plus:productAverage %}
{% assign reviewVotes = reviewVotes | plus:productVotes %}
{% assign reviewedProducts = reviewedProducts | plus:1 %}
{% endfor %}
{% comment %} Set the average based on product count {% endcomment %}
{% assign reviewedAllAverage = reviewAverage | divided_by:paginate.items | round: 1 %}
{% comment %} Set the average based on products with a review {% endcomment %}
{% assign reviewedOnlyAverage = reviewAverage | divided_by:reviewedProducts | round: 1 %}
<span class="collection-review-summary">
{% if useAll != blank and useAll != false %}
{{ collection.title }} Rating: {{ reviewedAllAverage }} out of {{ maxReview }} based on {{ reviewVotes }} {{ reviewVotes | pluralize: 'review', 'reviews' }}.
{% else %}
{{ collection.title }} Rating: {{ reviewedOnlyAverage }} out of {{ maxReview }} based on {{ reviewVotes }} {{ reviewVotes | pluralize: 'review', 'reviews' }}.
{% endif %}
</span>
{% endif %}
{% layout none %}{% capture WHITESPACE %}
{% comment %}
This is intended to be used as an alternate collection template.
Usage:
1. Add new collection template, adding this code.
2. View the collection https://yourstore.com/collections/foo?view=ratings-json
3. Read the output with JavaScript and add to your page as needed.
{% endcomment %}
{% comment %} Setup the basic vars {% endcomment %}
{% assign reviewAverage = 0 %}
{% assign totalReviews = 0 %}
{% assign reviewedProducts = 0 %}
{% comment %} Do not edit the two lines below unless you know what they do {% endcomment %}
{% assign splitMarkerAverage = 'itemprop="average" content="' %}
{% assign splitMarkerVotes = 'itemprop="votes" content="' %}
{% comment %} Add the usual pagination tags {% endcomment %}
{% paginate collection.products by 1000 %}
{% for product in collection.products %}
{% assign productReviewMetafield = product.metafields.spr.reviews %}
{% comment %} Some quick checks to see if the metafield exists, and has what we need. {% endcomment %}
{% if productReviewMetafield == blank %}{% continue %}{% endif %}
{% unless productReviewMetafield contains splitMarkerAverage and productReviewMetafield contains splitMarkerVotes %}{% continue %}{% endunless %}
{% comment %} Get the values from the metafield (hopefully)! {% endcomment %}
{% assign productAverage = productReviewMetafield | split:splitMarkerAverage | last | split:'"' | first | abs %}
{% assign productVotes = productReviewMetafield | split:splitMarkerVotes | last | split:'"' | first | abs %}
{% comment %} Update our values {% endcomment %}
{% assign reviewAverage = reviewAverage | plus:productAverage %}
{% assign totalReviews = totalReviews | plus:productVotes %}
{% assign reviewedProducts = reviewedProducts | plus:1 %}
{% endfor %}
{% comment %} The review average var is the combined amount for all products. Let's get the average and round that amount. {% endcomment %}
{% assign productsSearched = paginate.items %}
{% assign reviewAverage = reviewAverage | divided_by:productsSearched | round: 1 %}
{% endpaginate %}
{% endcapture %}{% if reviewAverage > 0 and totalReviews > 0 %}
{
"reviews":{
"totalReviews":{{ totalReviews | json }},
"reviewAverage": {{ reviewAverage | json }},
"reviewedProducts":{{ reviewedProducts | json }},
"productsSearched":{{ productsSearched | json }}
}
}{% endif %}
@emrass
Copy link

emrass commented Dec 30, 2016

Exactly what I was looking for - thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment