#How to retrieve shipping price?

28 messages ยท Page 1 of 1 (latest)

next orchid
#

We have region set at Medusa Admin but the cart API doesn't seem to return the shipping cost. How can I retrieve the delivery cost?

smoky lily
#

You need to check "Visible in store" in shipping option settings

smoky lily
#

Also you'll need to later add that shipping option to cart.

next orchid
#

@smoky lily what do you mean by saying "add that shipping option to cart"? Is that another field of cart?

smoky lily
next orchid
#

@smoky lily thank you!

next orchid
#

@smoky lily as far as I understand the delivery fee is going to appear once I add delivery address, correct?

smoky lily
#

no, you need to also add shipping method to cart

next orchid
#

So I need to set delivery address, extract new shipping_option[0] and set its ID at medusa.carts.addShippingMethod, correct?

smoky lily
#

Yes ๐Ÿ™‚

#

Tho I'm not sure about that delivery address, cart should be bound to region anyway. I don't think it's necessary, not sure why docs state that.

lofty raptor
#

If you are using a fulfilment provider plugin such as Webshipper, prices can depend on the address, as an example shipping providers in Denmark usually charge a higher price for addresses on the islands that aren't connected by bridge. It also affects what package lockers will be available if the provider offers that as a option etc.

But if you are just using shipping from Medusa without using a third party integration to handle shipping then @smoky lily is right that methods are only region specific, so you don't "need" to set a shipping address before adding a shipping method ๐Ÿ™‚

next orchid
#

so you don't "need" to set a shipping address before adding a shipping method
I'm sorry, but what do I need to do then to get the delivery price?

next orchid
#

@smoky lily @lofty raptor it works, thanks. But IMO it should set default shipping method so we don't make those requests.

lofty raptor
#

There is no such thing as a "default" shipping method, you could have many methods available such as home delivery, box delivery, express delivery etc, so I don't believe we should force that on our users that it picks one by default, you can add that yourself if you want to via a subscriber ๐Ÿ™‚

next orchid
#

Guys, why medusa.shippingOptions.listCartOptions(cartId) may return an empty shipping_options?

#

I mean shipping_options = []

#

we have it defined at medusa admin

next orchid
#

That's the cart object:

{
cart: {
object: 'cart',
id: 'cart_01GQJ2GGXT6RJZPD93G6SDX1Z1',
gift_cards: [],
region: {
id: 'reg_01GPB64YNSGT2N55W16W4E6PEJ',
created_at: '2023-01-09T11:55:14.467Z',
updated_at: '2023-01-09T11:55:14.467Z',
deleted_at: null,
name: 'Denmark',
currency_code: 'dkk',
tax_rate: 25,
tax_code: null,
gift_cards_taxable: true,
automatic_taxes: true,
tax_provider_id: null,
metadata: null,
countries: [Array],
payment_providers: [Array],
tax_rates: [Array],
fulfillment_providers: [Array]
},
items: [],
payment: null,
shipping_address: {
id: 'addr_01GQJ2GGXTPB7Q5M3F6M5C6MMH',
created_at: '2023-01-24T14:22:02.160Z',
updated_at: '2023-01-24T14:22:02.160Z',
deleted_at: null,
customer_id: null,
company: null,
first_name: null,
last_name: null,
address_1: null,
address_2: null,
city: null,
country_code: 'dk',
province: null,
postal_code: null,
phone: null,
metadata: null
},
billing_address: null,
shipping_methods: [],
deleted_at: null,
email: null,
billing_address_id: null,
shipping_address_id: 'addr_01GQJ2GGXTPB7Q5M3F6M5C6MMH',
region_id: 'reg_01GPB64YNSGT2N55W16W4E6PEJ',
customer_id: null,
payment_id: null,
type: 'default',
completed_at: null,
payment_authorized_at: null,
idempotency_key: null,
metadata: null,
subtotal: 0,
discount_total: 0,
item_tax_total: 0,
shipping_total: 0,
shipping_tax_total: 0,
gift_card_total: 0,
gift_card_tax_total: 0,
tax_total: 0,
total: 0
}
}

#

In other words, region and address (because of country_code) is correct but still the shipping options are not available for some reason.

#

@lofty raptor @smoky lily ๐Ÿ™

lofty raptor
#

Please don't ping people, see this message #announcements message

I am not sure if you are misunderstanding how it works, or I am just reading your message wrong. Calling medusa.shippingOptions.listCartOptions(cartId) won't populate the shipping_options array of the cart, it will return an array of options that are available to that cart, that you can then use to set the shipping option on the cart, eg:

const SelectShipping = ({ cartId }: { cartId: string }) => {
const [options, setOptions] = useState([])

useEffect(() => {
  const getOptions = async () => {
    const { shipping_options } = await medusa.shippingOptions.listCartOptions(cartId)

    setOptions(shipping_options)
  }

  getOptions()
}, [cartId])


const handleSelectOption = (optId: string) => {
medusa.carts.addShippingMethod(cartId, {
  option_id: optId
})
}

return (
  <div>
    {options.map((opt) => (<button type="button" onClick={() => handleSelectOption(opt.id)}></button>))}
  </div>
)
}
next orchid
#

@lofty raptor sorry for pinging. I understand how it works. shipping_options is now an empty array for some reason, that's the issue.

lofty raptor
#

Which products do you have in your cart? And what ShippingProfile do they have?

#

Yea it won't return shipping options for a empty cart, as it uses the ShippingProfile of the products as part of deciding which options would be valid for the cart.

next orchid
#

OMG, thanks a lot!