Magento 2 How to set shipping and billing address on the cart using GraphQL

We all konw that shipping and billing addresses are stored in quote_address table in magento 2, in this article teach you, how to set shipping and billing address on the cart using GraphQL.

Set Shipping Address on cart

We will use setShippingAddressesOnCart mutation sets one or more shipping addresses on the cart. The shipping address does not need to specify when the sets billing address is same_as_shipping and the cart contains only virtual items.

Syntax

mutation {
  setShippingAddressesOnCart(
    input: SetShippingAddressesOnCartInput
  ) {
    SetShippingAddressesOnCartOutput
  }
}

Now, we will show you example for set shipping address on cart.

Request

saveCopyzoom_out_map
mutation { setShippingAddressesOnCart( input: { cart_id: "2U1u9wHHOFNR3UAnBWMTznJfyKA74ZIc", shipping_addresses: [ { address:{ firstname: "Developer" lastname: "Admin" company: "Developer Company" country_code: "IN" city: "Ahmedabad" street: ["Ahmedabad"] region: "GJ" postcode: "380015" telephone: "0123456789" save_in_address_book: false }, } ] } ){ cart{ shipping_addresses{ firstname company country{ label code } city street region{ label code } postcode telephone } } } }

Response

saveCopyzoom_out_map
{ "data": { "setShippingAddressesOnCart": { "cart": { "shipping_addresses": [ { "firstname": "Developer", "company": "Developer Company", "country": { "label": "IN", "code": "IN" }, "city": "Ahmedabad", "street": [ "Ahmedabad" ], "region": { "label": "Gujarat", "code": "GJ" }, "postcode": "380015", "telephone": "0123456789" } ] } } } }

Set Billing Address on cart

We will use setBillingAddressOnCart mutation sets billing address on the cart. If we set the same_as_shipping attribute to true, Magento assigns the same address as the shipping address.

Syntax

mutation {
  setBillingAddressOnCart(
    input: SetBillingAddressOnCartInput
  ) {
    SetBillingAddressOnCartOutput
  }
}

In this exaample, we set new billing address on the cart.

Request

saveCopyzoom_out_map
mutation { setBillingAddressOnCart( input: { cart_id: "2U1u9wHHOFNR3UAnBWMTznJfyKA74ZIc" billing_address: { address: { firstname: "Developer" lastname: "Test" company: "Developer Test Company" country_code: "IN" city: "Ahmedabad" street: ["Ahmedabad"] region: "GJ" postcode: "380015" telephone: "0123456789" save_in_address_book: false } same_as_shipping: false } } ) { cart { billing_address { firstname lastname company country { label code } city street region { label code } postcode telephone } } } }

Response

saveCopyzoom_out_map
{ "data": { "setBillingAddressOnCart": { "cart": { "billing_address": { "firstname": "Developer", "lastname": "Test", "company": "Developer Test Company", "country": { "label": "IN", "code": "IN" }, "city": "Ahmedabad", "street": [ "Ahmedabad" ], "region": { "label": "Gujarat", "code": "GJ" }, "postcode": "380015", "telephone": "0123456789" } } } } }

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.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off