diff --git a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts index e696215fe7..62aa79aa7b 100644 --- a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts @@ -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", }), - ]), + ], }) ) }) diff --git a/packages/core/core-flows/src/definition/cart/steps/index.ts b/packages/core/core-flows/src/definition/cart/steps/index.ts index 0369a90973..9b18ae5170 100644 --- a/packages/core/core-flows/src/definition/cart/steps/index.ts +++ b/packages/core/core-flows/src/definition/cart/steps/index.ts @@ -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" diff --git a/packages/core/core-flows/src/definition/cart/steps/remove-shipping-method-from-cart.ts b/packages/core/core-flows/src/definition/cart/steps/remove-shipping-method-from-cart.ts new file mode 100644 index 0000000000..d0118cfb34 --- /dev/null +++ b/packages/core/core-flows/src/definition/cart/steps/remove-shipping-method-from-cart.ts @@ -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( + 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) + } +) diff --git a/packages/core/core-flows/src/definition/cart/workflows/add-shipping-method-to-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/add-shipping-method-to-cart.ts index 6f9bd34dde..8f7445962c 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/add-shipping-method-to-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/add-shipping-method-to-cart.ts @@ -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, }) ) }