Skip to content

Commit

Permalink
render json ld to products metadata sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
hannessolo committed Sep 18, 2024
1 parent 40fe81e commit 382d9bc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
38 changes: 38 additions & 0 deletions tools/pdp-metadata/pdp-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ async function performCatalogServiceQuery(config, query, variables) {
return queryResponse.data;
}

function getJsonLd(product) {
const amount = product.priceRange?.minimum?.final?.amount || product.price?.final?.amount;
const brand = product.attributes.find((attr) => attr.name === 'brand');

const schema = {
'@context': 'http://schema.org',
'@type': 'Product',
name: product.name,
description: product.meta_description,
image: product['og:image'],
offers: [],
productID: product.sku,
sku: product.sku,
url: product.path,
'@id': product.path,
};

if (brand?.value) {
product.brand = {
'@type': 'Brand',
name: brand?.value,
};
}

if (amount?.value && amount?.currency) {
schema.offers.push({
'@type': 'http://schema.org/Offer',
price: amount?.value,
priceCurrency: amount?.currency,
availability: product.inStock ? 'http://schema.org/InStock' : 'http://schema.org/OutOfStock',
});
}

return JSON.stringify(schema);
}

/**
* Get products by page number
* @param {INT} pageNumber - pass the pagenumber to retrieved paginated results
Expand Down Expand Up @@ -130,6 +166,7 @@ const getProducts = async (config, pageNumber) => {
'og:url',
'og:image',
'og:image:secure_url',
'json-ld',
],
];
products.forEach(({ productView: metaData }) => {
Expand All @@ -145,6 +182,7 @@ const getProducts = async (config, pageNumber) => {
`${basePath}${metaData.path}`, // og:url
metaData['og:image'], // og:image
metaData['og:image:secure_url'], // og:image:secure_url
getJsonLd(metaData), // json-ld
],
);
});
Expand Down
38 changes: 37 additions & 1 deletion tools/pdp-metadata/queries/products.graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,32 @@ export default `query productSearch($currentPage: Int = 1) {
sku
name
urlKey
url
shortDescription
description
metaDescription
metaKeyword
metaTitle
inStock
attributes(roles: []) {
name
value
}
... on SimpleProductView {
price {
...priceFields
}
}
... on ComplexProductView {
priceRange {
maximum {
...priceFields
}
minimum {
...priceFields
}
}
}
}
product {
image {
Expand All @@ -25,4 +46,19 @@ export default `query productSearch($currentPage: Int = 1) {
}
total_count
}
}`;
}
fragment priceFields on ProductViewPrice {
regular {
amount {
currency
value
}
}
final {
amount {
currency
value
}
}
}
`;

0 comments on commit 382d9bc

Please sign in to comment.