medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.65k stars 2.43k forks source link

It is possible to change shipping address after adding a dynamically calculated shipping method #5407

Open Alexnortung opened 11 months ago

Alexnortung commented 11 months ago

Bug report

Describe the bug

It is possible for a customer to change their shipping address once they have created a shipping method. This behavior is in my opinion okay. The problem arises if the price is dynamically calculated, since changing the shipping address should also recalculate the shipping price.

System information

Medusa version (including plugins): ^1.16.1 Node.js version: 18 Database: Postgres Operating system: NixOS

Steps to reproduce the behavior

  1. Create a fulfillment provider service
  2. Make the canCalculate method return true
  3. Make the calculatePrice method 0 if the postal code on the cart's shipping address is '1234' and make it return 100 otherwise
  4. Start the medusa server
  5. In medusa admin create a shipping option that uses calculated for the newly created fulfillment provider.
  6. Create a cart
  7. set shipping and billing address where postal code is 1234
  8. Create a shipping method of the created shipping option
  9. Observe that the shipping price is 0
  10. change your postal code to something that is not 1234
  11. Observe that the shipping method is the same and the price has not changed.

Expected behavior

I would expect that the dynamically calculated shipping methods would either be destroyed or recalculated based on the new cart data.

leocabeza commented 7 months ago

I stumbled upon this as well, could you resolve the issue?

leocabeza commented 7 months ago

I tried this when updating the address:

    const updatedCart = await updateCart(cartId, data)

    if (updatedCart!.shipping_methods.length) {
      await updateCart(cartId, { shipping_methods: [] })
    }
    revalidateTag("cart")

But it's not possible to update the shipping_method of a cart

leocabeza commented 7 months ago

I think I got it working with this code:

    const updatedCart = await updateCart(cartId, data)

    if (updatedCart!.shipping_methods.length) {
      await setShippingMethod(updatedCart!.shipping_methods[0].shipping_option_id)
    }
    revalidateTag("cart")