Feat(Medusa): Allow custom shipping price on draft orders (#2531)
* create custom shipping option for cart to have custom shipping price on draft orders * Create moody-chefs-stare.md
This commit is contained in:
5
.changeset/moody-chefs-stare.md
Normal file
5
.changeset/moody-chefs-stare.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
Feat(Medusa): Allow custom shipping price on draft orders
|
||||
@@ -71,6 +71,52 @@ describe("/admin/draft-orders", () => {
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("creates a draft order with a custom shipping option price", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
email: "oli@test.dk",
|
||||
shipping_address: "oli-shipping",
|
||||
items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
quantity: 2,
|
||||
metadata: {},
|
||||
},
|
||||
],
|
||||
region_id: "test-region",
|
||||
customer_id: "oli-test",
|
||||
shipping_methods: [
|
||||
{
|
||||
option_id: "test-option",
|
||||
price: 500,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api.post("/admin/draft-orders", payload, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const draftOrderId = response.data.draft_order.id
|
||||
|
||||
const draftOrderResponse = await api.get(
|
||||
`/admin/draft-orders/${draftOrderId}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
expect(draftOrderResponse.status).toEqual(200)
|
||||
expect(draftOrderResponse.data.draft_order.cart.shipping_total).toEqual(
|
||||
500
|
||||
)
|
||||
})
|
||||
|
||||
it("creates a draft order with a billing address that is an AddressPayload and a shipping address that is an ID", async () => {
|
||||
const api = useApi()
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { ExtendedFindConfig, FindConfig } from "../types/common"
|
||||
import { DraftOrderCreateProps } from "../types/draft-orders"
|
||||
import { buildQuery } from "../utils"
|
||||
import CartService from "./cart"
|
||||
import CustomShippingOptionService from "./custom-shipping-option"
|
||||
import EventBusService from "./event-bus"
|
||||
import LineItemService from "./line-item"
|
||||
import ProductVariantService from "./product-variant"
|
||||
@@ -24,6 +25,7 @@ type InjectedDependencies = {
|
||||
lineItemService: LineItemService
|
||||
productVariantService: ProductVariantService
|
||||
shippingOptionService: ShippingOptionService
|
||||
customShippingOptionService: CustomShippingOptionService
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,6 +49,7 @@ class DraftOrderService extends TransactionBaseService {
|
||||
protected readonly lineItemService_: LineItemService
|
||||
protected readonly productVariantService_: ProductVariantService
|
||||
protected readonly shippingOptionService_: ShippingOptionService
|
||||
protected readonly customShippingOptionService_: CustomShippingOptionService
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
@@ -58,6 +61,7 @@ class DraftOrderService extends TransactionBaseService {
|
||||
lineItemService,
|
||||
productVariantService,
|
||||
shippingOptionService,
|
||||
customShippingOptionService,
|
||||
}: InjectedDependencies) {
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(arguments[0])
|
||||
@@ -70,6 +74,7 @@ class DraftOrderService extends TransactionBaseService {
|
||||
this.cartService_ = cartService
|
||||
this.productVariantService_ = productVariantService
|
||||
this.shippingOptionService_ = shippingOptionService
|
||||
this.customShippingOptionService_ = customShippingOptionService
|
||||
this.eventBus_ = eventBusService
|
||||
}
|
||||
|
||||
@@ -327,6 +332,16 @@ class DraftOrderService extends TransactionBaseService {
|
||||
}
|
||||
|
||||
for (const method of shipping_methods) {
|
||||
if (typeof method.price !== "undefined") {
|
||||
await this.customShippingOptionService_
|
||||
.withTransaction(transactionManager)
|
||||
.create({
|
||||
shipping_option_id: method.option_id,
|
||||
cart_id: createdCart.id,
|
||||
price: method.price,
|
||||
})
|
||||
}
|
||||
|
||||
await cartServiceTx.addShippingMethod(
|
||||
createdCart.id,
|
||||
method.option_id,
|
||||
|
||||
Reference in New Issue
Block a user