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:
1 parent
ea3d738823
commit
38d4a7db3d
3 files changed
+66
No files matched your search
@@ -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)
|
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 () => {
|
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()
|
const api = useApi()
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { ExtendedFindConfig, FindConfig } from "../types/common"
|
|||||||
import { DraftOrderCreateProps } from "../types/draft-orders"
|
import { DraftOrderCreateProps } from "../types/draft-orders"
|
||||||
import { buildQuery } from "../utils"
|
import { buildQuery } from "../utils"
|
||||||
import CartService from "./cart"
|
import CartService from "./cart"
|
||||||
|
import CustomShippingOptionService from "./custom-shipping-option"
|
||||||
import EventBusService from "./event-bus"
|
import EventBusService from "./event-bus"
|
||||||
import LineItemService from "./line-item"
|
import LineItemService from "./line-item"
|
||||||
import ProductVariantService from "./product-variant"
|
import ProductVariantService from "./product-variant"
|
||||||
@@ -24,6 +25,7 @@ type InjectedDependencies = {
|
|||||||
lineItemService: LineItemService
|
lineItemService: LineItemService
|
||||||
productVariantService: ProductVariantService
|
productVariantService: ProductVariantService
|
||||||
shippingOptionService: ShippingOptionService
|
shippingOptionService: ShippingOptionService
|
||||||
|
customShippingOptionService: CustomShippingOptionService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,6 +49,7 @@ class DraftOrderService extends TransactionBaseService {
|
|||||||
protected readonly lineItemService_: LineItemService
|
protected readonly lineItemService_: LineItemService
|
||||||
protected readonly productVariantService_: ProductVariantService
|
protected readonly productVariantService_: ProductVariantService
|
||||||
protected readonly shippingOptionService_: ShippingOptionService
|
protected readonly shippingOptionService_: ShippingOptionService
|
||||||
|
protected readonly customShippingOptionService_: CustomShippingOptionService
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
manager,
|
manager,
|
||||||
@@ -58,6 +61,7 @@ class DraftOrderService extends TransactionBaseService {
|
|||||||
lineItemService,
|
lineItemService,
|
||||||
productVariantService,
|
productVariantService,
|
||||||
shippingOptionService,
|
shippingOptionService,
|
||||||
|
customShippingOptionService,
|
||||||
}: InjectedDependencies) {
|
}: InjectedDependencies) {
|
||||||
// eslint-disable-next-line prefer-rest-params
|
// eslint-disable-next-line prefer-rest-params
|
||||||
super(arguments[0])
|
super(arguments[0])
|
||||||
@@ -70,6 +74,7 @@ class DraftOrderService extends TransactionBaseService {
|
|||||||
this.cartService_ = cartService
|
this.cartService_ = cartService
|
||||||
this.productVariantService_ = productVariantService
|
this.productVariantService_ = productVariantService
|
||||||
this.shippingOptionService_ = shippingOptionService
|
this.shippingOptionService_ = shippingOptionService
|
||||||
|
this.customShippingOptionService_ = customShippingOptionService
|
||||||
this.eventBus_ = eventBusService
|
this.eventBus_ = eventBusService
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,6 +332,16 @@ class DraftOrderService extends TransactionBaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const method of shipping_methods) {
|
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(
|
await cartServiceTx.addShippingMethod(
|
||||||
createdCart.id,
|
createdCart.id,
|
||||||
method.option_id,
|
method.option_id,
|
||||||
|
|||||||
Reference in new issue
Block a user