Magento 2 How to set Shipping Method on the cart using GraphQL

In this artical, we will show you how to set shipping method on the cart. The setShippingMethodsOnCart mutation sets one or more delivery methods on a cart.

Magento graphql supports the following delivery methods:

  • DHL
  • Federal Express
  • Flat Rate
  • Free Shipping
  • Best Way
  • United Parcel Service
  • United States Postal Service

Syntax

Copy
mutation: {
  setShippingMethodsOnCart(
    input: setShippingMethodsOnCartInput
  ) {
    setShippingMethodsOnCartOutput
  }
}

The following example sets the shipping address on the cart.

Request

Copy
mutation {
  setShippingMethodsOnCart(
    input: {
      cart_id: "2U1u9wHHOFNR3UAnBWMTznJfyKA74ZIc"
      shipping_methods: [
        {
          carrier_code: "flatrate"
          method_code: "flatrate"
        }
      ]
    }
  ){
    cart{
      shipping_addresses{
        selected_shipping_method{
          carrier_code
          carrier_title
          method_code
          method_title
          amount{
            value
            currency
          }
        }
      }
    }
  }
}

Response

Copy
{
  "data": {
    "setShippingMethodsOnCart": {
      "cart": {
        "shipping_addresses": [
          {
            "selected_shipping_method": {
              "carrier_code": "flatrate",
              "carrier_title": "Flat Rate",
              "method_code": "flatrate",
              "method_title": "Fixed",
              "amount": {
                "value": 10,
                "currency": "USD"
              }
            }
          }
        ]
      }
    }
  }
}

That's it!!

If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.

I hope this article helps you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.



Search
Recent Posts
    Tags
    Newsletter
    SocialFollow on Twitter
    Visit now!