From 182d9a2fddfa2afc7b8d328bab16f56af71a32e6 Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Wed, 7 Oct 2020 21:58:36 +1030 Subject: [PATCH] Adding reviews to structured data --- routes/index.js | 5 ++++- views/layouts/layout.hbs | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/routes/index.js b/routes/index.js index dc25d6f5..6668e0a5 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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([ @@ -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; @@ -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, diff --git a/views/layouts/layout.hbs b/views/layouts/layout.hbs index c14e0c86..c32ea935 100644 --- a/views/layouts/layout.hbs +++ b/views/layouts/layout.hbs @@ -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}}