diff --git a/src/config.js b/src/config.js index b0f27d7..580afd3 100644 --- a/src/config.js +++ b/src/config.js @@ -11,6 +11,7 @@ */ import { errorWithResponse } from './utils/http.js'; +import { siteOverrides } from './overrides/index.js'; /** * This function finds ordered matches between a list of patterns and a given path. @@ -104,7 +105,9 @@ export async function resolveConfig(ctx, overrides = {}) { org, site, route, + siteOverrides: siteOverrides[siteKey], ...overrides, }; + return resolved; } diff --git a/src/content/helix-commerce.js b/src/content/helix-commerce.js index 8438a20..a194fe1 100644 --- a/src/content/helix-commerce.js +++ b/src/content/helix-commerce.js @@ -28,7 +28,7 @@ export async function handle(ctx, config) { } const product = await fetchProduct(ctx, config, sku); - const html = HTML_TEMPLATE(product, product.variants); + const html = HTML_TEMPLATE(config, product, product.variants); return new Response(html, { status: 200, headers: { diff --git a/src/overrides/index.js b/src/overrides/index.js new file mode 100644 index 0000000..b99d037 --- /dev/null +++ b/src/overrides/index.js @@ -0,0 +1,31 @@ +/* + * Copyright 2024 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +export const siteOverrides = { + 'thepixel--bul-eds': { + constructProductUrl: (config, product, variant) => { + const { host, matchedPath } = config; + const productPath = matchedPath + .replace('{{urlkey}}', product.urlKey) + .replace('{{sku}}', encodeURIComponent(product.sku.toLowerCase())); + + const productUrl = `${host}${productPath}`; + + if (variant) { + const options = variant.selections.map((selection) => atob(selection)).join(',').replace(/configurable\//g, '').replace(/\//g, '-'); + return `${productUrl}?pid=${variant.externalId}&o=${btoa(options)}`; + } + + return productUrl; + }, + }, +}; diff --git a/src/templates/json-ld.js b/src/templates/json-ld.js index dab7e4f..a45a6cf 100644 --- a/src/templates/json-ld.js +++ b/src/templates/json-ld.js @@ -30,7 +30,8 @@ export default (config, product, variants) => { prices, } = product; - const productUrl = constructProductUrl(config, product); + const productUrl = config.siteOverrides?.constructProductUrl(config, product) + ?? constructProductUrl(config, product); const image = images?.[0]?.url ?? findProductImage(product, variants)?.url; const brandName = attributes?.find((attr) => attr.name === 'brand')?.value; return JSON.stringify(pruneUndefined({ @@ -53,7 +54,8 @@ export default (config, product, variants) => { priceCurrency: prices?.final?.currency, }) : undefined, ...variants.map((v) => { - const offerUrl = constructProductUrl(config, product, v); + const offerUrl = config.siteOverrides?.constructProductUrl(config, product, v) + ?? constructProductUrl(config, product, v); const offer = { '@type': 'Offer', diff --git a/src/types.d.ts b/src/types.d.ts index d473b31..353de19 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -38,6 +38,7 @@ declare global { matchedPath: string; matchedPathConfig: Config; attributeOverrides: AttributeOverrides; + siteOverrides: Record>; } export interface Env {