From 3adeab142d8aca08c7e74172018ccb954393ea17 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 3 Jun 2021 13:44:14 +0100 Subject: [PATCH] Added Product data to theme middleware refs https://github.com/TryGhost/Team/issues/708 - Defaults to an empty array on `@products` so we have valid data (product should be null if products isn't) - This is the first step toward supporting multiple products at the theme level --- .../services/theme-engine/middleware.js | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/core/frontend/services/theme-engine/middleware.js b/core/frontend/services/theme-engine/middleware.js index c76a59dbcf..d3233ffa51 100644 --- a/core/frontend/services/theme-engine/middleware.js +++ b/core/frontend/services/theme-engine/middleware.js @@ -111,6 +111,19 @@ async function haxGetMembersPriceData() { } } +async function getProductAndPricesData() { + try { + const page = await api.canary.productsPublic.browse({ + include: ['monthly_price', 'yearly_price'], + limit: 'all' + }); + + return page.products; + } catch (err) { + return []; + } +} + function getSiteData(req) { let siteData = settingsCache.getPublic(); @@ -140,6 +153,15 @@ async function updateGlobalTemplateOptions(req, res, next) { image_sizes: activeTheme.get().config('image_sizes') }; const priceData = await haxGetMembersPriceData(); + const productData = await getProductAndPricesData(); + + let products = null; + let product = null; + if (productData.length === 1) { + product = productData[0]; + } else { + products = productData; + } // @TODO: only do this if something changed? // @TODO: remove blog in a major where we are happy to break more themes @@ -150,7 +172,9 @@ async function updateGlobalTemplateOptions(req, res, next) { site: siteData, labs: labsData, config: themeData, - price: priceData + price: priceData, + product, + products } }); }