fix(core-flows): handle missing variants and preserve zero unit_price in prepareLineItems (#13179)

* fix: handle missing variants and preserve zero unit_price in prepareLineItems

Fixed two issues in prepareLineItems:
1. unit_price value of 0 was previously treated as falsy and overwritten with the variant's price. Updated the check to only fallback when unitPrice is null or undefined.
2. When no variants array was provided, the code could throw due to non-null assertions. Now safely handles missing or empty variants.

Additional adjustments:
- Replaced `||` with `??` for array defaults to preserve valid empty arrays.
- Kept changes minimal to avoid altering function signature.

* Create sweet-wasps-build.md

* Update add-line-items.ts

* Update packages/core/core-flows/src/order/workflows/add-line-items.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* Update packages/core/core-flows/src/order/workflows/create-order.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

---------

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Abdulrahman
2025-08-18 15:50:25 +03:00
committed by GitHub
parent fd4e791428
commit eb376eb4cf
3 changed files with 14 additions and 9 deletions

View File

@@ -31,7 +31,7 @@ import { productVariantsFields } from "../utils/fields"
function prepareLineItems(data) {
const items = (data.input.items ?? []).map((item) => {
const variant = data.variants?.find((v) => v.id === item.variant_id)!
const variant = data.variants?.find((v) => v.id === item.variant_id)
const input: PrepareLineItemDataInput = {
item,
@@ -41,11 +41,11 @@ function prepareLineItems(data) {
item.is_tax_inclusive ??
variant?.calculated_price?.is_calculated_price_tax_inclusive,
isCustomPrice: isDefined(item?.unit_price),
taxLines: item.tax_lines || [],
adjustments: item.adjustments || [],
taxLines: item.tax_lines ?? [],
adjustments: item.adjustments ?? [],
}
if (variant && !input.unitPrice) {
if (variant && !isDefined(input.unitPrice)) {
input.unitPrice = variant.calculated_price?.calculated_amount
}

View File

@@ -33,21 +33,21 @@ import { updateOrderTaxLinesWorkflow } from "./update-tax-lines"
function prepareLineItems(data) {
const items = (data.input.items ?? []).map((item) => {
const variant = data.variants.find((v) => v.id === item.variant_id)!
const variant = data.variants?.find((v) => v.id === item.variant_id)
const input: PrepareLineItemDataInput = {
item,
variant: variant,
unitPrice: item.unit_price ?? undefined,
unitPrice: item.unit_price,
isTaxInclusive:
item.is_tax_inclusive ??
variant?.calculated_price?.is_calculated_price_tax_inclusive,
isCustomPrice: isDefined(item?.unit_price),
taxLines: item.tax_lines || [],
adjustments: item.adjustments || [],
taxLines: item.tax_lines ?? [],
adjustments: item.adjustments ?? [],
}
if (variant && !input.unitPrice) {
if (variant && !isDefined(input.unitPrice)) {
input.unitPrice = variant.calculated_price?.calculated_amount
}