fix(core-flows): Avoid throwing if no prices found for variant when adding to cart custom price item (#14528)
* Avoid throwing prices not found error when line item is custom unit price * Add changeset * Avoid throwing upo variant price validation for custom priced item variants
This commit is contained in:
@@ -91,13 +91,15 @@ async function fetchVariantPriceSets(
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that all variants have price sets and throws error for missing ones
|
||||
* Validates that all variants without a custom price have price sets and throws error for missing ones
|
||||
*/
|
||||
function validateVariantPriceSets(
|
||||
variantPriceSets: VariantPriceSetData[]
|
||||
variantPriceSets: VariantPriceSetData[],
|
||||
variantsWithCustomPrice: string[] = []
|
||||
): void {
|
||||
const variantsWithCustomPriceSet = new Set(variantsWithCustomPrice)
|
||||
const notFound = variantPriceSets
|
||||
.filter((v) => !v.price_set?.id)
|
||||
.filter((v) => !v.price_set?.id && !variantsWithCustomPriceSet.has(v.id))
|
||||
.map((v) => v.id)
|
||||
|
||||
if (notFound.length) {
|
||||
@@ -287,7 +289,10 @@ export const getVariantPriceSetsStep = createStep(
|
||||
const variantIds = bulkData.map((item) => item.variantId)
|
||||
const variantPriceSets = await fetchVariantPriceSets(query, variantIds)
|
||||
|
||||
validateVariantPriceSets(variantPriceSets)
|
||||
const variantsWithCustomPrice = bulkData
|
||||
.filter((item) => !!item.context?.is_custom_price)
|
||||
.map((item) => item.variantId)
|
||||
validateVariantPriceSets(variantPriceSets, variantsWithCustomPrice)
|
||||
|
||||
// Map variant IDs to price set IDs
|
||||
const variantToPriceSetId = new Map<string, string>()
|
||||
|
||||
@@ -125,6 +125,7 @@ export const getVariantsAndItemsWithPrices = createWorkflow(
|
||||
context: {
|
||||
...baseContext,
|
||||
quantity: item.quantity,
|
||||
is_custom_price: !!item.unit_price,
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -180,7 +181,10 @@ export const getVariantsAndItemsWithPrices = createWorkflow(
|
||||
calculatedPriceSet = calculatedPriceSets[item_.variant_id!]
|
||||
}
|
||||
|
||||
if (!calculatedPriceSet && item_.variant_id) {
|
||||
const isCustomPrice =
|
||||
item_.is_custom_price ?? isDefined(item?.unit_price)
|
||||
|
||||
if (!calculatedPriceSet && item_.variant_id && !isCustomPrice) {
|
||||
priceNotFound.push(item_.variant_id)
|
||||
}
|
||||
|
||||
@@ -198,9 +202,6 @@ export const getVariantsAndItemsWithPrices = createWorkflow(
|
||||
variant.calculated_price = calculatedPriceSet
|
||||
}
|
||||
|
||||
const isCustomPrice =
|
||||
item_.is_custom_price ?? isDefined(item?.unit_price)
|
||||
|
||||
const input: PrepareLineItemDataInput = {
|
||||
item: item_,
|
||||
variant: variant,
|
||||
|
||||
Reference in New Issue
Block a user