-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interim commit of schema.org support.
- Loading branch information
1 parent
6e6c9d4
commit b863664
Showing
4 changed files
with
138 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
examples/commerce-essentials/src/components/product/schema/ProductSchema.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { ShopperProduct } from "@elasticpath/react-shopper-hooks"; | ||
import type { Offer, Product, ProductGroup, WithContext, Thing } from "schema-dts"; | ||
interface IProductSchema { | ||
product: ShopperProduct; | ||
} | ||
|
||
const ProductSchema = ({ product }: IProductSchema): JSX.Element => { | ||
const { | ||
response, | ||
} = product; | ||
|
||
let schema = {}; | ||
|
||
if (product.kind === "child-product") { | ||
|
||
const productGroupSchema: ProductGroup = { | ||
"@type": "ProductGroup", | ||
"@id": product.baseProduct.response.attributes.slug, | ||
productGroupID: product.baseProduct.response.attributes.sku, | ||
name: product.baseProduct.response.attributes.name, | ||
description: product.baseProduct.response.attributes.description, | ||
sku: product.baseProduct.response.attributes.sku, | ||
image: product.baseProduct.main_image?.link.href, | ||
// @ts-ignore - support for custom options | ||
variesBy: product.baseProduct.meta?.variations?.map((variation) => variation.name), | ||
}; | ||
|
||
const offers: Offer[] = buildOffers(product); | ||
const productSchema: Product = buildProduct(product, offers) | ||
productSchema.isVariantOf = { | ||
"@id": product.baseProduct.response.attributes.slug, | ||
}; | ||
// @ts-ignore - support for custom options | ||
productSchema.options = []; | ||
// @ts-ignore - child_variations is supported but missing from the sdk type. | ||
response.meta.child_variations.forEach((variation) => { | ||
if (variation.name.toLowerCase() === "color") { | ||
productSchema.color = variation.option.name; | ||
|
||
} else if (variation.name.toLowerCase() === "size") { | ||
productSchema.size = variation.option.name; | ||
} else { | ||
// @ts-ignore - support for custom options | ||
productSchema.options.push({ | ||
"@type": "PropertyValue", | ||
name: variation.name, | ||
value: variation.option.name, | ||
}); | ||
} | ||
}) | ||
|
||
productGroupSchema.hasVariant = [productSchema]; | ||
schema = addContext(productGroupSchema); | ||
} else if (product.kind === "simple-product") { | ||
|
||
const offers: Offer[] = buildOffers(product); | ||
const productSchema: Product = buildProduct(product, offers) | ||
|
||
schema = addContext(productSchema); | ||
} | ||
|
||
return ( | ||
<script id="product-schema" type="application/ld+json" | ||
dangerouslySetInnerHTML={{ | ||
__html: schema, | ||
}} /> | ||
); | ||
}; | ||
|
||
export default ProductSchema; | ||
|
||
function addContext(productSchema: Product) { | ||
return JsonLd<Product>({ | ||
"@context": "https://schema.org", | ||
...productSchema, | ||
}); | ||
} | ||
|
||
export function JsonLd<T extends Thing>(json: WithContext<T>): string { | ||
return JSON.stringify(json); | ||
} | ||
|
||
function buildProduct(product: ShopperProduct, offers: Offer[]): Product { | ||
const response = product.response; | ||
return { | ||
"@type": "Product", | ||
name: response.attributes.name, | ||
description: response.attributes.description, | ||
sku: response.attributes.sku, | ||
mpn: response.attributes.manufacturer_part_num, | ||
image: product.main_image?.link.href, | ||
offers, | ||
}; | ||
} | ||
|
||
function buildOffers(product: ShopperProduct): Offer[] { | ||
if (!product.response.attributes.price) { | ||
return []; | ||
} | ||
return Object.keys(product.response.attributes.price).map(key => { | ||
return { | ||
"@type": "Offer", | ||
price: product.response.attributes.price[key].amount, | ||
priceCurrency: key, | ||
}; | ||
}); | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.