medusajs/nextjs-starter-medusa

calculatePrice for dynamic shipping cost no longer working

shm0x opened this issue · 2 comments

Hi, since the new starter version, the fulfillment provider that use calculated pricing for shipping is no longer working.

On a fulfillment service it's possible to dynamically compute the shipping cost :

` canCalculate() {
return true;
}
And

calculatePrice(optionData: any, data: any, cart: any) {
// compute shipping cost

}`

But since the rewrite that removed react contexts, all shipping methods that use calculated pricing return 0 and it's only updated after we select the shipping method. Not really good user experience.

The calculatePrice is not triggered until we select a shipping method.

Is there anyway to calculate shipping cost for all the fulfillment provider on checkout, like it was before ?

Thanks.

Ok, seems that new version is fetching all shipping methods for a region, not for a specific cart.

I fixed it by creating this to fetch shipping options insted of listShippingMethods

export async function listCartOptions(cartId: string) {
  const headers = getMedusaHeaders(["cart"])

  return medusaClient.shippingOptions
    .listCartOptions(cartId, headers)
    .then(({ shipping_options }) => shipping_options)
    .catch((err) => {
      console.log(err)
      return null
    })
}

Good catch, and thanks for sharing your solution!