Skip to content

Commit

Permalink
fix(core-flows): copy line items attribute (medusajs#8471)
Browse files Browse the repository at this point in the history
removing `requires_shipping` from variant won't be part of this PR
  • Loading branch information
carlos-r-l-rodrigues authored Aug 6, 2024
1 parent 04044c9 commit 3d7dce2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/core-flows/src/definition/cart/utils/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -95,6 +96,9 @@ export const productVariantsFields = [
"sku",
"manage_inventory",
"allow_backorder",
"requires_shipping",
"is_discountable",
"variant_option_values",
"barcode",
"product.id",
"product.title",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
BigNumberInput,
CartLineItemDTO,
CreateOrderAdjustmentDTO,
CreateOrderLineItemTaxLineDTO,
ProductVariantDTO,
} from "@medusajs/types"

interface Input {
item?: CartLineItemDTO
quantity: BigNumberInput
metadata?: Record<string, any>
unitPrice: BigNumberInput
Expand All @@ -18,6 +20,7 @@ interface Input {

export function prepareLineItemData(data: Input) {
const {
item,
variant,
unitPrice,
isTaxInclusive,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand All @@ -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,
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/types/src/product/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/api/admin/orders/query-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const defaultAdminRetrieveOrderFields = [
"total",
"subtotal",
"tax_total",
"order_change",
"discount_total",
"discount_tax_total",
"original_total",
Expand Down

0 comments on commit 3d7dce2

Please sign in to comment.