Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render json ld to products metadata sheet #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a SimpleProductView we can add a single offer like done here. For a ComplexProductView we should add all available variants.

For this we can use a query like this:

{
  variants(sku: "MJ12", optionIds: [], pageSize: 100) {
    variants {
      selections
      product {
        sku
        inStock
        __typename
        images(roles: "image") {
          url
        }
        ... on SimpleProductView {
          price {
            ...priceFields
          }
        }
      }
    }
    cursor
  }
}

If desired by the customer this could also be limited to specific variant axis (e.g. show color variants, but not the size ones).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this was done in the client-side setJsonLd so far:

Should we update the client side function too?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I think we were actually waiting for the variants query to be available on prod CS. If you can re-use the same code, feel free to update PDP as well. Otherwise we can also handle it in a separate issue.

'@type': 'http://schema.org/Offer',
hannessolo marked this conversation as resolved.
Show resolved Hide resolved
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
hannessolo marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
}
`;
Loading