GraphQL checkout tutorial

Step 6. Set the shipping method

The setShippingMethodsOnCart mutation defines the shipping methods for your order. It requires these input parameters:

  • cart_id
  • carrier_code
  • method_code

{ CART_ID } is the unique shopping cart ID from Step 2. Create empty cart.

For logged-in customers, send the customer’s authorization token in the Authorization parameter of the header. See “Get customer authorization token” for more information.

Request:

The following mutation query assigns UPS “Ground” method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mutation {
  setShippingMethodsOnCart(input: {
    cart_id: "{ CART_ID }"
    shipping_methods: [
      {
        carrier_code: "ups"
        method_code: "GND"
      }
    ]
  }) {
    cart {
      shipping_addresses {
        selected_shipping_method {
          carrier_code
          method_code
          carrier_title
          method_title
        }
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "data": {
    "setShippingMethodsOnCart": {
      "cart": {
        "shipping_addresses": [
          {
            "selected_shipping_method": {
              "carrier_code": "ups",
              "method_code": "GND",
              "carrier_title": "United Parcel Service",
              "method_title": "Ground"
            }
          }
        ]
      }
    }
  }
}

Verify this step

  1. Sign in as a customer to the website using the email john.doe@example.com and password b1b2b3l@w+.

  2. Go to Checkout.

  3. The selected shipping method is displayed in the Shipping Methods section on the Shipping step.