4
0
forked from crowetic/commerce
2021-01-12 16:59:07 -03:00

21 lines
575 B
TypeScript

export type SelectedOptions = {
size: string | null
color: string | null
}
export function getVariant(product: Product, opts: SelectedOptions) {
const variant = product.variants.find((variant) => {
return Object.entries(opts).every(([key, value]) =>
variant.options.find((option) => {
if (
option.__typename === 'MultipleChoiceOption' &&
option.displayName.toLowerCase() === key.toLowerCase()
) {
return option.values.find((v) => v.label.toLowerCase() === value)
}
})
)
})
return variant
}