updateBuyerIdentityInCart method

Future<Cart> updateBuyerIdentityInCart({
  1. required String cartId,
  2. required CartBuyerIdentityInput buyerIdentity,
})

update Buyer identity in cart

Implementation

Future<Cart> updateBuyerIdentityInCart({
  required String cartId,
  required CartBuyerIdentityInput buyerIdentity,
}) async {
  final deliveryAddressPreferences = buyerIdentity.deliveryAddressPreferences;
  List<Map<String, dynamic>> deliveryAddressPreferencesData = [];
  for (var pref in deliveryAddressPreferences) {
    if (pref != null) {
      if (pref.deliveryAddress != null && pref.customerAddressId == null) {
        deliveryAddressPreferencesData.add({
          'deliveryAddress': pref.deliveryAddress?.toJson() ?? {},
        });
      } else if (pref.customerAddressId != null &&
          pref.deliveryAddress == null) {
        deliveryAddressPreferencesData.add({
          'customerAddressId': pref.customerAddressId,
        });
      } else if (pref.customerAddressId != null &&
          pref.deliveryAddress != null) {
        throw Exception(
          'Customer Address Id and Delivery Address cannot be set at the same time, please choose one',
        );
      }
    }
  }
  final MutationOptions updateBuyerIdentity = MutationOptions(
    document: gql(cartBuyerIdentityUpdate),
    variables: {
      'cartId': cartId,
      'buyerIdentity': {
        'email': buyerIdentity.email,
        'phone': buyerIdentity.phone,
        'countryCode': buyerIdentity.countryCode,
        'customerAccessToken': buyerIdentity.customerAccessToken,
        'deliveryAddressPreferences': deliveryAddressPreferencesData,
      },
      'country': ShopifyLocalization.countryCode,
    },
  );
  QueryResult result = await _graphQLClient!.mutate(updateBuyerIdentity);
  checkForError(result,
      key: 'cartBuyerIdentityUpdate', errorKey: 'userErrors');

  return Cart.fromJson(
      ((result.data!['cartBuyerIdentityUpdate'] ?? const {})['cart'] ??
          const {}));
}