Calculate shipping rate with draft order api

Solved
zivmiz
Shopify Partner
3 0 1

Hi,

I'm developing a custom app and I'm trying to create a draft order with the calculation of shipping via API.

The problem is that I don't have the shipping rates options.

How can I get the shipping rates of a specific draft order?

like there is in checkout api?

Accepted Solution (1)

Accepted Solutions
Brett13
Tourist
4 1 4

This is an accepted solution.

I did find a way to get this working.  Take a look at the Checkouts API.  There is an endpoint to shipping_rates.json that returns the shipping rates.  All you need is the checkout token.  I was able to get this from the draft order's invoice URL. For example, draft order invoice URL: https://apple.myshopify.com/690933842/invoices/d64d93d528cfc75c0876183d58f4a has checkout token: d64d93d528cfc75c0876183d58f4a.

https://shopify.dev/docs/admin-api/rest/reference/sales-channels/checkout?api[version]=2020-04

Screen Shot 2020-08-04 at 11.38.27 AM.png

View solution in original post

Replies 14 (14)
_JB
Shopify Staff
Shopify Staff
836 99 211

Hey @zivmiz,

 

Just wanted to let you know we're looking in to this. The documentation definitely makes it seem like you can calculate shipping rates using this endpoint, but to be honest I don't remember ever seeing that and I'm not seeing a way to do it now. I'll post back here when I have an update for you.

JB | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Brett13
Tourist
4 1 4

I'm also facing this issue.  I need to get the shipping rates for a draft order to be able to set the shipping_line of the draft order.  Any updates on this?

nepthar
Tourist
6 0 3

Hey guys,

Just want to throw my two cents in here! I've also been trying to get this to work based on what I read in the docs, but I can't figure it out. Seems like I'm not alone! Any word on a possible fix?

Brett13
Tourist
4 1 4

This is an accepted solution.

I did find a way to get this working.  Take a look at the Checkouts API.  There is an endpoint to shipping_rates.json that returns the shipping rates.  All you need is the checkout token.  I was able to get this from the draft order's invoice URL. For example, draft order invoice URL: https://apple.myshopify.com/690933842/invoices/d64d93d528cfc75c0876183d58f4a has checkout token: d64d93d528cfc75c0876183d58f4a.

https://shopify.dev/docs/admin-api/rest/reference/sales-channels/checkout?api[version]=2020-04

Screen Shot 2020-08-04 at 11.38.27 AM.png

nepthar
Tourist
6 0 3

Thanks for that follow up! I'm going to give it a try now. Out of curiosity, are you a "Sales Channel"?

Brett13
Tourist
4 1 4

Sure no problem.  No, I'm not a sales channel.  Just a software developer from Unific dealing with this issue myself.

NiceTeam
Excursionist
15 0 12

The problem with the solution here is that this endpoint is only available to Sales Channels. It can't be used by normal apps.
As far as I know, there is still no way to get the shipping rate for draft order without specifying it by yourself.

tolgapaksoy
Shopify Partner
91 7 55

Use the Storefront API to fetch the shipping rates on the frontend/client.

We use this exact trick with DraftOrder API, and use the checkout token in the invoice URL as a checkout ID. This checkout ID is used client-side to query for shipping rates.

NiceTeam
Excursionist
15 0 12

@tolgapaksoyThank you for this information. It seems like this should work 🙂 Will try it out.

Lilante
New Member
2 0 0

There is also another option. Use this code snippet:

      "shipping_line": {
            "custom":"true",
            "title":"Fedex",
            "price":"12.99"
        }
 
nepthar
Tourist
6 0 3

Can you explain what you mean by that? Are you suggesting we just charge everyone 12.99 for "Fedex"? 😄

gorodezkiy
Shopify Expert
6 0 12

For our regular private app I could get available shipping rates by using GraphQL Admin API - draftOrderCalculate mutation - https://shopify.dev/docs/admin-api/graphql/reference/orders/draftordercalculate#interactive-example-... :

Query POST https://jennyyoo-staging.myshopify.com/admin/api/2020-01/graphql.json :

mutation draftOrderCalculate($input: DraftOrderInput!) {
  draftOrderCalculate(input: $input) {
    calculatedDraftOrder {
      subtotalPrice
      totalPrice
      totalShippingPrice
      totalTax
      availableShippingRates {
          handle
          title
          price {
              amount
          }
      }
    }
    userErrors {
      field
      message
    }
  }
}

 

Input:

{
    "input": {
        "lineItems": [{
            "variantId": "gid://shopify/ProductVariant/123",
            "quantity": 1,
            "appliedDiscount": null
        }],
        "shippingAddress": {
            "firstName": "Eduard",
            "lastName": "...",
            "address1": "...",
            "address2": "...",
            "phone": "123",
            "city": "Miami",
            "countryCode": "US",
            "provinceCode": "FL",
            "zip": "123"
        },
        "billingAddress": {
            "firstName": "Eduard",
            "lastName": "...",
            "address1": "...",
            "address2": "...",
            "phone": "123",
            "city": "Miami",
            "countryCode": "US",
            "provinceCode": "FL",
            "zip": "123"
        },
        "email": "eduard@example.com"
    }
}

 

Response:

{
    "data": {
        "draftOrderCalculate": {
            "calculatedDraftOrder": {
                "subtotalPrice": "285.00",
                "totalPrice": "319.20",
                "totalShippingPrice": "0.00",
                "totalTax": "34.20",
                "availableShippingRates": [
                    {
                        "handle": "shopify-USPS%20First%20Class%20Mail-2.75",
                        "title": "USPS First Class Mail",
                        "price": {
                            "amount": "2.75"
                        }
                    },
                    {
                        "handle": "shopify-Fedex%20Home%20Delivery-9.95",
                        "title": "Fedex Home Delivery",
                        "price": {
                            "amount": "9.95"
                        }
                    },
                    {
                        "handle": "fedex-FEDEX_EXPRESS_SAVER-22.67",
                        "title": "FedEx FedEx Express Saver®",
                        "price": {
                            "amount": "22.67"
                        }
                    },
                    {
                        "handle": "fedex-FEDEX_2_DAY-30.84",
                        "title": "FedEx FedEx 2Day®",
                        "price": {
                            "amount": "30.84"
                        }
                    },
                    {
                        "handle": "fedex-STANDARD_OVERNIGHT-46.24",
                        "title": "FedEx FedEx Standard Overnight®",
                        "price": {
                            "amount": "46.24"
                        }
                    }
                ]
            },
            "userErrors": []
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 11,
            "actualQueryCost": 11,
            "throttleStatus": {
                "maximumAvailable": 2000.0,
                "currentlyAvailable": 1989,
                "restoreRate": 100.0
            }
        }
    }
}

 

ApiPaul
Tourist
4 0 1

Thanks @gorodezkiy you rocks!

 
 
 
Skylitech
Shopify Partner
5 0 3

Hi, 

Do you have any solution to this?

We need this.

There is no way to calculate to fetch available shipping options with draft order API.