How To Shipping Address in Cart By Using GraphQL in Magento 2.x / Adobe Commerce 2.x


The following below steps need to follow to add shipping address in Cart by using GraphQL in Magento 2.x / Adobe Commerce 2.x

Here, we are using the setShippingAddressesOnCart mutation to add a shipping address to the cart.

Below use for guest checkout & logged customer checkout, in case of logged checkout need to use customer authorization

Step [1] – Recommendation to use below syntax to to Use Shipping address in Cart

Here Using Postman

Syntax

mutation {
  setShippingAddressesOnCart(
    input: {
      cart_id: "{ CART_ID }"
      shipping_addresses: [
        {
          address: {
            firstname: "Customer First Name"
            lastname: "Customer Lastt Name"
            company: "Company Name"
            street: ["Street Address"]
            city: "Customer City"
            region: "Customer Region"
            region_id: Customer Region ID
            postcode: "Customer Post Code"
            country_code: "Customer Country Code"
            telephone: "Customer Telephone"
            save_in_address_book: false or true
          }
        }
      ]
    }
  ) {
    cart {
      shipping_addresses {
        firstname
        lastname
        company
        street
        city
        region {
          code
          label
        }
        postcode
        telephone
        country {
          code
          label
        }
        available_shipping_methods{
          carrier_code
          carrier_title
          method_code
          method_title
        }
      }
    }
  }
}

Step [2] – Put below Request

mutation {
  setShippingAddressesOnCart(
    input: {
      cart_id: "s6Wgi4j0H2ULX0YHfD3mhOMHuAL10qLL"
      shipping_addresses: [
        {
          address: {
            firstname: "John"
            lastname: "Doe"
            company: "Company Name"
            street: ["3320 N Crescent Dr", "Beverly Hills"]
            city: "Los Angeles"
            region: "CA"
            region_id: 12
            postcode: "90210"
            country_code: "US"
            telephone: "123-456-0000"
            save_in_address_book: false
          }
        }
      ]
    }
  ) {
    cart {
      shipping_addresses {
        firstname
        lastname
        company
        street
        city
        region {
          code
          label
        }
        postcode
        telephone
        country {
          code
          label
        }
        available_shipping_methods{
          carrier_code
          carrier_title
          method_code
          method_title
        }
      }
    }
  }
}

Step [3] – Put below Output Response

{
    "data": {
        "setShippingAddressesOnCart": {
            "cart": {
                "shipping_addresses": [
                    {
                        "firstname": "John",
                        "lastname": "Doe",
                        "company": "Company Name",
                        "street": [
                            "3320 N Crescent Dr",
                            "Beverly Hills"
                        ],
                        "city": "Los Angeles",
                        "region": {
                            "code": "CA",
                            "label": "California"
                        },
                        "postcode": "90210",
                        "telephone": "123-456-0000",
                        "country": {
                            "code": "US",
                            "label": "US"
                        },
                        "available_shipping_methods": [
                            {
                                "carrier_code": "flatrate",
                                "carrier_title": "Flat Rate",
                                "method_code": "flatrate",
                                "method_title": "Fixed"
                            }
                        ]
                    }
                ]
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *