fix(cart): add shipping method to cart (#7945)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-04 09:47:39 -03:00
committed by GitHub
parent f4225c051f
commit d036130604
4 changed files with 64 additions and 4 deletions

View File

@@ -1500,6 +1500,21 @@ medusaIntegrationTestRunner({
},
})
await addShippingMethodToWorkflow(appContainer).run({
input: {
options: [{ id: shippingOption.id }],
cart_id: cart.id,
},
})
// should remove the previous shipping method
await addShippingMethodToWorkflow(appContainer).run({
input: {
options: [{ id: shippingOption.id }],
cart_id: cart.id,
},
})
cart = await cartModuleService.retrieveCart(cart.id, {
relations: ["shipping_methods"],
})
@@ -1508,12 +1523,12 @@ medusaIntegrationTestRunner({
expect.objectContaining({
id: cart.id,
currency_code: "usd",
shipping_methods: expect.arrayContaining([
shipping_methods: [
expect.objectContaining({
amount: 3000,
name: "Test shipping option",
}),
]),
],
})
)
})

View File

@@ -18,6 +18,7 @@ export * from "./prepare-adjustments-from-promotion-actions"
export * from "./refresh-cart-shipping-methods"
export * from "./remove-line-item-adjustments"
export * from "./remove-shipping-method-adjustments"
export * from "./remove-shipping-method-from-cart"
export * from "./retrieve-cart"
export * from "./retrieve-cart-with-links"
export * from "./set-tax-lines-for-items"

View File

@@ -0,0 +1,35 @@
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
shipping_method_ids: string[]
}
export const removeShippingMethodFromCartStepId =
"remove-shipping-method-to-cart-step"
export const removeShippingMethodFromCartStep = createStep(
removeShippingMethodFromCartStepId,
async (data: StepInput, { container }) => {
const cartService = container.resolve<ICartModuleService>(
ModuleRegistrationName.CART
)
const methods = await cartService.softDeleteShippingMethods(
data.shipping_method_ids
)
return new StepResponse(methods, data.shipping_method_ids)
},
async (ids, { container }) => {
if (!ids) {
return
}
const cartService: ICartModuleService = container.resolve(
ModuleRegistrationName.CART
)
await cartService.restoreShippingMethods(ids)
}
)

View File

@@ -7,6 +7,7 @@ import {
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
import {
addShippingMethodToCartStep,
removeShippingMethodFromCartStep,
validateCartShippingOptionsStep,
} from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
@@ -76,7 +77,15 @@ export const addShippingMethodToWorkflow = createWorkflow(
}
)
const shippingMethods = addShippingMethodToCartStep({
const currentShippingMethods = transform({ cart }, ({ cart }) => {
return cart.shipping_methods.map((sm) => sm.id)
})
removeShippingMethodFromCartStep({
shipping_method_ids: currentShippingMethods,
})
const shippingMethodsToAdd = addShippingMethodToCartStep({
shipping_methods: shippingMethodInput,
})
@@ -84,7 +93,7 @@ export const addShippingMethodToWorkflow = createWorkflow(
refreshCartPromotionsStep({ id: input.cart_id }),
updateTaxLinesStep({
cart_or_cart_id: input.cart_id,
shipping_methods: shippingMethods,
shipping_methods: shippingMethodsToAdd,
})
)
}