fix(medusa): Calculated price on cart shipping options (#1878)
This commit is contained in:
+1
@@ -27,6 +27,7 @@ describe("GET /store/shipping-options", () => {
|
||||
relations: [
|
||||
"region",
|
||||
"items",
|
||||
"items.adjustments",
|
||||
"items.variant",
|
||||
"items.variant.product",
|
||||
],
|
||||
|
||||
@@ -33,10 +33,17 @@ export default async (req, res) => {
|
||||
|
||||
const cart = await cartService.retrieve(cart_id, {
|
||||
select: ["subtotal"],
|
||||
relations: ["region", "items", "items.variant", "items.variant.product"],
|
||||
relations: [
|
||||
"region",
|
||||
"items",
|
||||
"items.adjustments",
|
||||
"items.variant",
|
||||
"items.variant.product",
|
||||
],
|
||||
})
|
||||
|
||||
const options = await shippingProfileService.fetchCartOptions(cart)
|
||||
|
||||
const data = await pricingService.setShippingOptionPrices(options, {
|
||||
cart_id,
|
||||
})
|
||||
|
||||
@@ -263,7 +263,7 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
||||
const methodRepo = manager.getCustomRepository(this.methodRepository_)
|
||||
|
||||
if (typeof config.cart !== "undefined") {
|
||||
this.validateCartOption(option, config.cart)
|
||||
await this.validateCartOption(option, config.cart)
|
||||
}
|
||||
|
||||
const validatedData = await this.providerService_.validateFulfillmentData(
|
||||
@@ -328,10 +328,10 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
||||
* @param {Cart} cart - the cart object to check against
|
||||
* @return {ShippingOption} the validated shipping option
|
||||
*/
|
||||
validateCartOption(
|
||||
async validateCartOption(
|
||||
option: ShippingOption,
|
||||
cart: Cart
|
||||
): ShippingOption | null {
|
||||
): Promise<ShippingOption | null> {
|
||||
if (option.is_return) {
|
||||
return null
|
||||
}
|
||||
@@ -365,6 +365,8 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
||||
)
|
||||
}
|
||||
|
||||
option.amount = await this.getPrice_(option, option.data, cart)
|
||||
|
||||
return option
|
||||
}
|
||||
|
||||
|
||||
@@ -461,20 +461,25 @@ class ShippingProfileService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
const options = []
|
||||
|
||||
for (const o of rawOpts) {
|
||||
try {
|
||||
const option = this.shippingOptionService_.validateCartOption(o, cart)
|
||||
if (option) {
|
||||
options.push(option)
|
||||
const options = await Promise.all(
|
||||
rawOpts.map(async (so) => {
|
||||
try {
|
||||
const option = await this.shippingOptionService_.validateCartOption(
|
||||
so,
|
||||
cart
|
||||
)
|
||||
if (option) {
|
||||
return option
|
||||
}
|
||||
return null
|
||||
} catch (err) {
|
||||
// if validateCartOption fails it means the option is not valid
|
||||
return null
|
||||
}
|
||||
} catch (ex) {
|
||||
// catch the error, but intentionally do not break the iterations
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return options
|
||||
return options.filter(Boolean)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user