From 3d7dce2bec183a0a4b0a42731664c6c7629337e2 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 6 Aug 2024 15:58:08 -0300 Subject: [PATCH] fix(core-flows): copy line items attribute (#8471) removing `requires_shipping` from variant won't be part of this PR --- .../__tests__/cart/store/carts.spec.ts | 1 + .../src/definition/cart/utils/fields.ts | 5 +++ .../cart/utils/prepare-line-item-data.ts | 39 ++++++++++------ .../cart/workflows/complete-cart.ts | 44 ++++++++++++++++--- packages/core/types/src/product/common.ts | 4 ++ .../src/api/admin/orders/query-config.ts | 1 + 6 files changed, 74 insertions(+), 20 deletions(-) diff --git a/integration-tests/modules/__tests__/cart/store/carts.spec.ts b/integration-tests/modules/__tests__/cart/store/carts.spec.ts index 549fb49383dd0..cb6838993249c 100644 --- a/integration-tests/modules/__tests__/cart/store/carts.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/carts.spec.ts @@ -2032,6 +2032,7 @@ medusaIntegrationTestRunner({ original_shipping_total: 0, items: [ expect.objectContaining({ + id: expect.stringContaining("ordli_"), product_id: product.id, unit_price: 100, quantity: 1, diff --git a/packages/core/core-flows/src/definition/cart/utils/fields.ts b/packages/core/core-flows/src/definition/cart/utils/fields.ts index 811264641e6d9..44c41bc19cfff 100644 --- a/packages/core/core-flows/src/definition/cart/utils/fields.ts +++ b/packages/core/core-flows/src/definition/cart/utils/fields.ts @@ -79,6 +79,7 @@ export const completeCartFields = [ "payment_collection.*", "payment_collection.payment_sessions.*", "items.variant.id", + "items.variant.product.id", "items.variant.manage_inventory", "items.variant.allow_backorder", "items.variant.inventory_items.inventory_item_id", @@ -95,6 +96,9 @@ export const productVariantsFields = [ "sku", "manage_inventory", "allow_backorder", + "requires_shipping", + "is_discountable", + "variant_option_values", "barcode", "product.id", "product.title", @@ -104,6 +108,7 @@ export const productVariantsFields = [ "product.type", "product.collection", "product.handle", + "product.discountable", "calculated_price.calculated_amount", "calculated_price.is_calculated_price_tax_inclusive", "inventory_items.inventory_item_id", diff --git a/packages/core/core-flows/src/definition/cart/utils/prepare-line-item-data.ts b/packages/core/core-flows/src/definition/cart/utils/prepare-line-item-data.ts index 8ac814c63c3ce..291d8bbfc515f 100644 --- a/packages/core/core-flows/src/definition/cart/utils/prepare-line-item-data.ts +++ b/packages/core/core-flows/src/definition/cart/utils/prepare-line-item-data.ts @@ -1,11 +1,13 @@ import { BigNumberInput, + CartLineItemDTO, CreateOrderAdjustmentDTO, CreateOrderLineItemTaxLineDTO, ProductVariantDTO, } from "@medusajs/types" interface Input { + item?: CartLineItemDTO quantity: BigNumberInput metadata?: Record unitPrice: BigNumberInput @@ -18,6 +20,7 @@ interface Input { export function prepareLineItemData(data: Input) { const { + item, variant, unitPrice, isTaxInclusive, @@ -34,23 +37,31 @@ export function prepareLineItemData(data: Input) { const lineItem: any = { quantity, - title: variant.title, + title: variant.title ?? item?.title, + subtitle: variant.product.title ?? item?.subtitle, + thumbnail: variant.product.thumbnail ?? item?.thumbnail, - subtitle: variant.product.title, - thumbnail: variant.product.thumbnail, - - product_id: variant.product.id, - product_title: variant.product.title, - product_description: variant.product.description, - product_subtitle: variant.product.subtitle, - product_type: variant.product.type?.[0]?.value ?? null, - product_collection: variant.product.collection?.[0]?.value ?? null, - product_handle: variant.product.handle, + product_id: variant.product.id ?? item?.product_id, + product_title: variant.product.title ?? item?.product_title, + product_description: + variant.product.description ?? item?.product_description, + product_subtitle: variant.product.subtitle ?? item?.product_subtitle, + product_type: + variant.product.type?.[0]?.value ?? item?.product_type ?? null, + product_collection: + variant.product.collection?.[0]?.value ?? + item?.product_collection ?? + null, + product_handle: variant.product.handle ?? item?.product_handle, variant_id: variant.id, - variant_sku: variant.sku, - variant_barcode: variant.barcode, - variant_title: variant.title, + variant_sku: variant.sku ?? item?.variant_sku, + variant_barcode: variant.barcode ?? item?.variant_barcode, + variant_title: variant.title ?? item?.variant_title, + variant_option_values: item?.variant_option_values, + + is_discountable: variant.product.discountable ?? item?.is_discountable, + requires_shipping: variant.requires_shipping ?? item?.requires_shipping, unit_price: unitPrice, is_tax_inclusive: !!isTaxInclusive, diff --git a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts index b9ef4b05fdabc..b21f99686e059 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts @@ -17,6 +17,11 @@ import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-pa import { validateCartPaymentsStep } from "../steps" import { reserveInventoryStep } from "../steps/reserve-inventory" import { completeCartFields } from "../utils/fields" +import { + prepareAdjustmentsData, + prepareLineItemData, + prepareTaxLinesData, +} from "../utils/prepare-line-item-data" import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory" export const completeCartWorkflowId = "complete-cart" @@ -80,11 +85,38 @@ export const completeCartWorkflow = createWorkflow( ) const cartToOrder = transform({ input, cart }, ({ cart }) => { - const itemAdjustments = (cart.items || []) - ?.map((item) => item.adjustments || []) + const allItems = (cart.items ?? []).map((item) => { + return prepareLineItemData({ + item, + variant: item.variant, + unitPrice: item.raw_unit_price ?? item.unit_price, + isTaxInclusive: item.is_tax_inclusive, + quantity: item.raw_quantity ?? item.quantity, + metadata: item?.metadata, + taxLines: item.tax_lines ?? [], + adjustments: item.adjustments ?? [], + }) + }) + + const shippingMethods = (cart.shipping_methods ?? []).map((sm) => { + return { + name: sm.name, + description: sm.description, + amount: sm.raw_amount ?? sm.amount, + is_tax_inclusive: sm.is_tax_inclusive, + shipping_option_id: sm.shipping_option_id, + data: sm.data, + metadata: sm.metadata, + tax_lines: prepareTaxLinesData(sm.tax_lines ?? []), + adjustments: prepareAdjustmentsData(sm.adjustments ?? []), + } + }) + + const itemAdjustments = allItems + .map((item) => item.adjustments ?? []) .flat(1) - const shippingAdjustments = (cart.shipping_methods || []) - ?.map((sm) => sm.adjustments || []) + const shippingAdjustments = shippingMethods + .map((sm) => sm.adjustments ?? []) .flat(1) const promoCodes = [...itemAdjustments, ...shippingAdjustments] @@ -101,8 +133,8 @@ export const completeCartWorkflow = createWorkflow( shipping_address: cart.shipping_address, billing_address: cart.billing_address, no_notification: false, - items: cart.items, - shipping_methods: cart.shipping_methods, + items: allItems, + shipping_methods: shippingMethods, metadata: cart.metadata, promo_codes: promoCodes, } diff --git a/packages/core/types/src/product/common.ts b/packages/core/types/src/product/common.ts index 6d88717085a5d..d084c6fd11c4f 100644 --- a/packages/core/types/src/product/common.ts +++ b/packages/core/types/src/product/common.ts @@ -192,6 +192,10 @@ export interface ProductVariantDTO { * Whether the product variant's inventory should be managed by the core system. */ manage_inventory: boolean + /** + * Whether the product variant's requires shipping. + */ + requires_shipping: boolean /** * The HS Code of the product variant. */ diff --git a/packages/medusa/src/api/admin/orders/query-config.ts b/packages/medusa/src/api/admin/orders/query-config.ts index 5c173920a0c0e..9a60ced5eef58 100644 --- a/packages/medusa/src/api/admin/orders/query-config.ts +++ b/packages/medusa/src/api/admin/orders/query-config.ts @@ -18,6 +18,7 @@ export const defaultAdminRetrieveOrderFields = [ "total", "subtotal", "tax_total", + "order_change", "discount_total", "discount_tax_total", "original_total",