Retrieving The Product Reviews Number from the Product Reviews App, but directly in the product liquid file.

Tourist
9 0 2

The product reviews shopify app allows to display the number of reviews of a product with the following syntax:

{{product.reviews_count}}

This work if the syntax is placed within the app.

I tried to take the same line of code and put it directly in my liquid file, but it doesn't work. (Technically it seems to be like any other attribute, just like "id" or  "variant") but it's not supported directly within the liquid itself.

 

Anybody would know how to make it work? Thanks

 

0 Likes
Shopify Expert
2674 65 664

Hi g, 

Here is a snippet I use in my theme (not very optimized, though)

{% assign product__reviews-count=0 %}
{% if product.metafields.spr.reviews %}
          {% capture review_data %}{{ product.metafields.spr.reviews }}{% endcapture %}
          {% assign rd_a1 = review_data | split: '"votes"' %} 
          {% assign rd_a2 = rd_a1[1] | split: "=" %}
          {% assign rd_a3 = rd_a2[1] | split: '"' %}
          {% assign product__reviews-count = rd_a3[1] | plus: 1 | minus: 1 %}
{% endif %}

I use it like this 

{% include 'get-spr-reviews-data' %}

{% if product__reviews-count <= 0 %}
   {% assign reviews-tab__header="Review" %}
{% else %}
  {% capture reviews-tab__header %}{{product__reviews-count}} {{product__reviews-count | pluralize: 'review', 'reviews'}}{% endcapture %}   
{% endif %}

And this is how it works  in online jewellery shop

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
0 Likes
Shopify Partner
23 0 5

This isn't working for me. I just copy your code and place it on my product.liquid template. But It isn't working. Here is my code

{% assign product__reviews-count=0 %}
{% if product.metafields.spr.reviews %}
          {% capture review_data %}{{ product.metafields.spr.reviews }}{% endcapture %}
          {% assign rd_a1 = review_data | split: '"votes"' %} 
          {% assign rd_a2 = rd_a1[1] | split: "=" %}
          {% assign rd_a3 = rd_a2[1] | split: '"' %}
          {% assign product__reviews-count = rd_a3[1] | plus: 1 | minus: 1 %}
{% endif %}


{% if product__reviews-count <= 0 %}
   {% assign reviews-tab__header="0 Review" %}
{% else %}
  {% capture reviews-tab__header %}{{product__reviews-count}} {{product__reviews-count | pluralize: 'review', 'reviews'}}{% endcapture %}   
{% endif %}
{{ reviews-tab__header }}

 

0 Likes
Shopify Expert
2674 65 664

The Product Reviews app has changed since that post, I had to revise the code:

{% assign product__reviews-count=0 %}
{% if product.metafields.spr.reviews %}

          {% capture review_data %}{{ product.metafields.spr.reviews }}{% endcapture %}

          {% if review_data contains '"votes"' %}
              {% assign keyword = '"votes"' %}
          {% else  %}
              {% assign keyword = '"reviewCount"' %}
          {% endif %}

          {% assign rd_a1 = review_data | split: keyword %} 
		  {% assign rd_a2 = rd_a1[1] | split: "=" %}
          {% assign rd_a3 = rd_a2[1] | split: '"' %}
          {% assign product__reviews-count = rd_a3[1] | plus: 1 | minus: 1 %}

{% endif %}
Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
4 Likes
Shopify Partner
23 0 5

Great Reply. It's now working like a charm. Thanks Tim. You are really a brilliant person.

0 Likes
New Member
1 0 0

Hi Tim, thanks for your input on this one, this is a great solution.

I have just implemented the code you provided as I wanted to use the number of reviews as a trigger to hide a whole section of the product page if no reviews are present. (The other methods provided for this don't work to hide an entire section of the page).

The reviews section is wrapped in an if statement:

 {{ if product__reviews-count != 0 }} 

 <div class="reviews> .... </div>

 {{ endif }}


1. If the product has reviews, it shows the section.
2. If the product has no reviews, it hides the section.

There is one small problem though,  {{product__reviews-count }} will only report 0 reviews accurately if the product was created as a brand new product. If the product was created as a duplicate of another product that has 1 or more reviews, it will return the number of reviews for the product that was duplicated!

The result is that the reviews section of page is showing up for products that have no reviews, because the product they were duplicated from has reviews. 

Do you know of any way to fix this using script? Unfortunately there is no way to effectively sort which products are affected, so I'm hoping there is a way the  {{ if product__reviews-count }} can be forced to update?

Otherwise it will mean manually trawling through each product (there's about 600 of them) and creating new versions of any that are affected. 

0 Likes
Shopify Expert
2674 65 664

Hi Dan, 

unfortunately,  I have no definite answer to this question at the moment.

Yes, Shopify Product Reviews App is storing some info in the product metafield and we are able to read this information and extract the required data. When we make a copy of the product, its metafields are copied as well.

However, looks like SPR only updates this metafield only when the actual poroducts reviews are updated.

So I'd be thinking about clearing the metafield immediately after the product copy is made (the ShopifyFD is a nice tool here); using .CSV export/import to create a product copy is another possible way.

It could be that you can talk to Shopify and there is a way to force the App to "sync" metafields with it's data. Otherwise, I'd try exporting all reiews to CSV, clearing all metafields (there are apps which allow for bulk metafield editing), deleting all the reviews and importing them back from the CSV, if you have large inventory. (I have not tested this, it's a good idea to try on a test product first!).

 

Another possible option for you is to hide the reviews section with Javascript after the SPR App updates the page with the actual data.

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
0 Likes
New Member
1 0 0

This actually worked. But one thing I noticed is when you delete a review, it does not update the product review that it shows. 

0 Likes
New Member
1 0 0

works perfect. thanks Tim

0 Likes

I did a small tweak to enable retrieving more than Product Reviews Number ie. to retrieve bestRating, ratingValue and worstRating score.
I implemented this in a client's store to inclue the review property within website's Schema Markup.

{% assign product_review_array = "reviewCount,bestRating,ratingValue,worstRating" | split: ','%}
{% if product.metafields.spr.reviews %}
	{% capture review_data %}{{ product.metafields.spr.reviews }}{% endcapture %}
	{% assign array_builder = "" %}
	{% for review_data_item in product_review_array %}
		{% capture new_var %}"{{ review_data_item }}"{% endcapture %}
		{% assign review_data1 = review_data | split: new_var %} 
		{% assign review_data2 = review_data1[1] | split: "=" %}
		{% assign review_data3 = review_data2[1] | split: '"' %}
		{% assign review_data_value = review_data3[1] | plus: 1 | minus: 1 %}
		{% assign array_builder = array_builder | append: review_data_value | append: '_' %}
	{% endfor %}
{% assign review_data_array = array_builder | split: '_' %}
  reviewCount:{{ review_data_array[0] }}
  bestRating:{{ review_data_array[1] }}  
  ratingValue:{{ review_data_array[2] }} 
  worstRating:{{ review_data_array[3] }}
{% endif %}

What this does is....
1. Sets an array for the terms you wish to get values for

{% assign product_review_array = "reviewCount,bestRating,ratingValue,worstRating" | split: ','%}

2. Checks to see if Shopify Reviews Object exists

{% if product.metafields.spr.reviews %}
...
{% endif %}

3. Loops thru all the items in 'product_review_array' then use 'split:' to extract the result we want eg. 5 or 100.

4. Appends each result to our 'array_builder' then splits the 'array_builder' to create 'review_data_array'

 

Was my answer helpful? Be sure to give it a "Like" and mark it as the solution - this way others can find it easily.
- Need help with a Shopify build or custom solution? Feel free to get in touch via hello@stonedigital.com.au !
- Did my reply help solve your problem? The above email is my Paypal too ʘ‿ʘ
1 Like