From 5acebd1046336a446ce17f08bb4c663fda0e2586 Mon Sep 17 00:00:00 2001 From: lakshmithi Date: Sat, 21 Dec 2024 21:07:14 +0530 Subject: [PATCH] show price api call integrated in commerce js --- blocks/product-hero/product-hero.js | 17 +++++++++--- scripts/commerce.js | 41 ++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/blocks/product-hero/product-hero.js b/blocks/product-hero/product-hero.js index e062e9f3..f6cbbb34 100644 --- a/blocks/product-hero/product-hero.js +++ b/blocks/product-hero/product-hero.js @@ -1,9 +1,9 @@ import { - a, div, p, span, hr, h1, + a, div, p, span, hr, h1, h4, } from '../../scripts/dom-builder.js'; import { getAuthorization, getCommerceBase, - getProductResponse, + getProductResponse, getProductPriceDetails } from '../../scripts/commerce.js'; import { createOptimizedS7Picture, decorateModals } from '../../scripts/scripts.js'; import { getMetadata } from '../../scripts/lib-franklin.js'; @@ -147,7 +147,7 @@ async function addToQuote(product) { referrer: window.location.href, referrerTitle: document.title.replace('| Danaher Lifesciences', '').replace('| Danaher Life Sciences', '').trim(), }), - }); + });console.log('phquote ', quote); const { default: getToast } = await import('../../scripts/toast.js'); if (quote.status === 200) { const responseJson = await quote.json(); @@ -163,12 +163,23 @@ async function addToQuote(product) { } } +async function addToCart(product){ + try { + const baseURL = getCommerceBase(); + const sku = getSKU(); + const showURL = `${baseURL} + /?product=${sku} `; + console.log('phshowURL ', showURL); + + }catch (error) {} +} + export default async function decorate(block) { const titleEl = block.querySelector('h1'); const h1Value = getMetadata('h1'); titleEl?.classList.add('title'); titleEl?.parentElement.parentElement.remove(); const response = await getProductResponse(); + const cartResponse = await getProductPriceDetails(); if (response?.length > 0) { const allImages = response[0]?.raw.images; const verticalImageGallery = imageSlider(allImages, response[0]?.Title); diff --git a/scripts/commerce.js b/scripts/commerce.js index b52f1a43..36713824 100644 --- a/scripts/commerce.js +++ b/scripts/commerce.js @@ -8,7 +8,7 @@ import { export function getCommerceBase() { return window.DanaherConfig !== undefined ? window.DanaherConfig.intershopDomain + window.DanaherConfig.intershopPath : 'https://shop.lifesciences.danaher.com/INTERSHOP/rest/WFS/DANAHERLS-LSIG-Site/-'; } - +console.log('brl ', getCommerceBase()); /** * Returns the user authorization used for commerce API calls */ @@ -138,6 +138,45 @@ export async function getProductResponse() { } } +export async function getProductPriceDetails() { + try{ + let response = JSON.parse(localStorage.getItem('product-details')); + const sku = getSKU(); + if (response && response.at(0)?.raw.sku === sku) { + return response; + } + localStorage.removeItem('product-details'); + + const host = `https://${window.DanaherConfig.host}/us/en/product-data`; + const url = window.location.search + ? `${host}/${window.location.search}&product=${sku}` + : `${host}/?product=${sku}`; + + const baseURL = getCommerceBase(); + let showURL = `${baseURL}/products/${sku} `; + showURL = showURL.replace(/-abcam/g, ""); + console.log('cshowURL ', showURL); + + const mockURL = 'https://stage.shop.lifesciences.danaher.com/INTERSHOP/rest/WFS/DANAHERLS-LSIG-Site/-/products/ab150686'; + const priceResponse = await fetch(mockURL) + .then((res) => { + if (res.ok) { + return res.json(); + } + throw new Error('Sorry, network error, not able to render response.'); + }); + + if (priceResponse.results.length > 0) { + response = priceResponse.results; + localStorage.setItem('product-details', JSON.stringify(priceResponse.results)); + return response; + } + } catch (error) { + // eslint-disable-next-line no-console + console.error(error); + } +} + function getWorkflowFamily() { if (window.location.pathname.match(/\/us\/en\/solutions\//)) { const pageUrl = window.location.pathname.replace(/^\/us\/en\/solutions\//, '').replace(/\.html$/, '').split('/');