Skip to content

Commit

Permalink
Adding reviews to structured data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvautin committed Oct 7, 2020
1 parent 83c42b3 commit 182d9a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ router.get('/product/:id', async (req, res) => {
// Grab review data
let reviews = [];
let reviewRating = 0;
let reviewCount = 0;
if(config.modules.enabled.reviews){
reviews = await db.reviews.find({ product: product._id }).sort({ date: 1 }).limit(6).toArray();
reviewRating = await db.reviews.aggregate([
Expand All @@ -433,6 +434,7 @@ router.get('/product/:id', async (req, res) => {
}
}
]).toArray();
reviewCount = await db.reviews.countDocuments({ product: product._id });
// Assign if returned
if(reviewRating.length > 0 && reviewRating[0].avgRating){
reviewRating = reviewRating[0].avgRating;
Expand Down Expand Up @@ -477,7 +479,8 @@ router.get('/product/:id', async (req, res) => {
result: product,
variants,
reviews,
reviewRating: reviewRating,
reviewRating,
reviewCount,
reviewRatingHtml: getRatingHtml(Math.round(reviewRating)),
images: images,
relatedProducts,
Expand Down
13 changes: 12 additions & 1 deletion views/layouts/layout.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,24 @@
"offers": {
"price": "{{result.productPrice}}",
"priceCurrency": "{{config.currencyISO}}",
{{#ifCond result.productStock '>' 0}}
"availability": "https://schema.org/InStock",
{{else}}
"availability": "https://schema.org/SoldOut",
{{/ifCond}}
"url": "{{pageUrl}}"
},
"sku": "{{result._id}}",
"publisher": "{{config.cartTitle}}",
{{#if result.productImage}}
"image": "{{config.baseUrl}}{{result.productImage}}",
{{/if}}
{{#if @root.reviewRating}}
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "{{@root.reviewRating}}",
"reviewCount": "{{@root.reviewCount}}"
},
{{/if}}
{{#if productDescription}}
"description": "{{snip productDescription}}",
{{/if}}
Expand Down

0 comments on commit 182d9a2

Please sign in to comment.