mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Supply Spree primary variant if a product has no option variants
This commit is contained in:
@@ -31,19 +31,20 @@ const normalizeProduct = (
|
|||||||
spreeSuccessResponse: SpreeSdkResponse,
|
spreeSuccessResponse: SpreeSdkResponse,
|
||||||
spreeProduct: ProductAttr
|
spreeProduct: ProductAttr
|
||||||
): Product => {
|
): Product => {
|
||||||
const primaryVariant = jsonApi.findSingleRelationshipDocument<VariantAttr>(
|
const spreePrimaryVariant =
|
||||||
spreeSuccessResponse,
|
jsonApi.findSingleRelationshipDocument<VariantAttr>(
|
||||||
spreeProduct,
|
spreeSuccessResponse,
|
||||||
'primary_variant'
|
spreeProduct,
|
||||||
)
|
'primary_variant'
|
||||||
|
)
|
||||||
|
|
||||||
if (primaryVariant === null) {
|
if (spreePrimaryVariant === null) {
|
||||||
throw new MissingPrimaryVariantError(
|
throw new MissingPrimaryVariantError(
|
||||||
`Couldn't find primary variant for product with id ${spreeProduct.id}.`
|
`Couldn't find primary variant for product with id ${spreeProduct.id}.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const sku = primaryVariant.attributes.sku
|
const sku = spreePrimaryVariant.attributes.sku
|
||||||
|
|
||||||
const price: ProductPrice = {
|
const price: ProductPrice = {
|
||||||
value: parseFloat(spreeProduct.attributes.price),
|
value: parseFloat(spreeProduct.attributes.price),
|
||||||
@@ -57,7 +58,6 @@ const normalizeProduct = (
|
|||||||
(requireConfigValue('showSingleVariantOptions') as boolean) ||
|
(requireConfigValue('showSingleVariantOptions') as boolean) ||
|
||||||
hasNonMasterVariants
|
hasNonMasterVariants
|
||||||
|
|
||||||
let variants: ProductVariant[]
|
|
||||||
let options: ExpandedProductOption[] = []
|
let options: ExpandedProductOption[] = []
|
||||||
|
|
||||||
const spreeVariantRecords = jsonApi.findRelationshipDocuments(
|
const spreeVariantRecords = jsonApi.findRelationshipDocuments(
|
||||||
@@ -66,38 +66,51 @@ const normalizeProduct = (
|
|||||||
'variants'
|
'variants'
|
||||||
)
|
)
|
||||||
|
|
||||||
variants = spreeVariantRecords.map((spreeVariantRecord) => {
|
// Use variants with option values if available. Fall back to
|
||||||
let variantOptions: ExpandedProductOption[] = []
|
// Spree primary_variant if no explicit variants are present.
|
||||||
|
const spreeOptionsVariantsOrPrimary =
|
||||||
|
spreeVariantRecords.length === 0
|
||||||
|
? [spreePrimaryVariant]
|
||||||
|
: spreeVariantRecords
|
||||||
|
|
||||||
if (showOptions) {
|
const variants: ProductVariant[] = spreeOptionsVariantsOrPrimary.map(
|
||||||
const spreeOptionValues = jsonApi.findRelationshipDocuments(
|
(spreeVariantRecord) => {
|
||||||
spreeSuccessResponse,
|
let variantOptions: ExpandedProductOption[] = []
|
||||||
spreeVariantRecord,
|
|
||||||
'option_values'
|
|
||||||
)
|
|
||||||
|
|
||||||
// Only include options which are used by variants.
|
if (showOptions) {
|
||||||
|
const spreeOptionValues = jsonApi.findRelationshipDocuments(
|
||||||
spreeOptionValues.forEach((spreeOptionValue) => {
|
|
||||||
variantOptions = expandOptions(
|
|
||||||
spreeSuccessResponse,
|
spreeSuccessResponse,
|
||||||
spreeOptionValue,
|
spreeVariantRecord,
|
||||||
variantOptions
|
'option_values'
|
||||||
)
|
)
|
||||||
|
|
||||||
options = expandOptions(spreeSuccessResponse, spreeOptionValue, options)
|
// Only include options which are used by variants.
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
spreeOptionValues.forEach((spreeOptionValue) => {
|
||||||
id: spreeVariantRecord.id,
|
variantOptions = expandOptions(
|
||||||
options: variantOptions,
|
spreeSuccessResponse,
|
||||||
|
spreeOptionValue,
|
||||||
|
variantOptions
|
||||||
|
)
|
||||||
|
|
||||||
|
options = expandOptions(
|
||||||
|
spreeSuccessResponse,
|
||||||
|
spreeOptionValue,
|
||||||
|
options
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: spreeVariantRecord.id,
|
||||||
|
options: variantOptions,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
|
|
||||||
const spreePrimaryVariantImageRecords = jsonApi.findRelationshipDocuments(
|
const spreePrimaryVariantImageRecords = jsonApi.findRelationshipDocuments(
|
||||||
spreeSuccessResponse,
|
spreeSuccessResponse,
|
||||||
primaryVariant,
|
spreePrimaryVariant,
|
||||||
'images'
|
'images'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user