0

I am using magento graphQL api to build mobile app of magento site. In checkout process we have to set billing address. So I used one of mutation setBillingAddressOnCart to set billing address on checkout. And I want to use billing address same as shipping so I passed same_as_shipping: true in billing_address. But when it's run, I am getting error :

Error: GraphQL error: The billing address must contain either "customer_address_id" or "address".

Here is mutation I used to set billing address :

  mutation setBillingAddressSameAsShipping($cartId: String!) {
    setBillingAddressOnCart(input: { cart_id: $cartId, billing_address: { same_as_shipping: true } }) {
      cart {
        id
        email
        shipping_addresses {
          selected_shipping_method {
            method_code
            method_title
          }
        }
      }
    }
  }

Please anyone can tell me why I am getting this error ? Why I have to pass customer_address_id even if I want to use billing address same as shipping ? And if I customer_address_id is really required than what for guest user ?

1 Answer 1

2

I have checked your mutation request parameters. First of all, this mutation is all about to set billing address for your specific quote (cart). So either you need to pass "customer_address_id (i.e. shipping address id in case you want to assign ready made address which is already saved before) or address fields (if you are creating a new address now).

Devdocs Reference :

enter image description here

I know that you need to just copy the shipping address into billing address. But GraphQL need either shipping address id which needed to copy into billing address or the whole new address object. It is not that smart that it will pick up your shipping address and copy into billing address automatically.

For guest user you need to pass the whole address object as below:

mutation {
  setBillingAddressOnCart(
    input: {
      cart_id: "4JQaNVJokOpFxrykGVvYrjhiNv9qt31C"
      billing_address: {
        address: {
          firstname: "Bob"
          lastname: "Roll"
          company: "Magento"
          street: ["Magento Pkwy", "Main Street"]
          city: "Austin"
          region: "TX"
          postcode: "78758"
          country_code: "US"
          telephone: "8675309"
          save_in_address_book: true
        }
        same_as_shipping: false
      }
    }
  ) {
    cart {
      billing_address {
        firstname
        lastname
        company
        street
        city
        region{
          code
          label
        }
        postcode
        telephone
        country{
          code
          label
        }
      }
    }
  }
}

I hope I made clear your doubts. Still, if you have any queries feel free to ask!

Thanks,

5
  • But in documentation customer_address_id or address are not displayed as required. Are you sure that there is no other solution without passing manually shipping address to keep billing address same as shipping ? Commented Sep 2, 2020 at 7:06
  • @KishanBharda, On Magento2 devdocs, they have provided example only for address object. But in the case you have saved shipping address for any logged in customer then you can just pass its address id. I have added devdocs screenshot in my answer. Please check they have mentioned the "customer_address_id" as one the possible input parameter. I have already implemented this feature in my project and it is working fine. Please let me know if you have any further queries. Thanks! Commented Sep 2, 2020 at 7:25
  • I know documentation mentioned customer_address_id as one the possible input parameter, but they does not mentioned as required parameter. So, is it possible to set billing address same as shipping address by just passing parameter same_as_shipping: true ? Commented Sep 2, 2020 at 7:35
  • Hello @KishanBharda, they have not actually mentioned about which parameter are mandatory or not. But one of these (customer_address_id or address object) parameter is required for "setBillingAddressOnCart" mutation in real time scenario. Commented Sep 2, 2020 at 8:15
  • Ok. Thank You. I managed to solve it. As you said I stored selected shipping address in local storage and passed to billing address. Commented Sep 2, 2020 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.