mirror of
https://github.com/vercel/commerce.git
synced 2025-07-28 12:41:22 +00:00
.vscode
assets
components
auth
cart
common
icons
product
ProductCard
ProductSlider
ProductView
Swatch
helpers.ts
index.ts
ui
wishlist
config
docs
framework
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
README.md
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
23 lines
623 B
TypeScript
23 lines
623 B
TypeScript
import type { Product } from '@commerce/types'
|
|
|
|
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
|
|
}
|