Alokai
You are reading the documentation for SDK v2, which is no longer the latest version. Switch to current

setCartBillingAddress

Sets the billing address for a cart in the CommerceTools project.

This method sends a POST request to the updateCart endpoint of the Vue Storefront API Middleware. In turn, it creates a cart for commercetools by sending a request to its GraphQL API. The default GraphQL query used by this method can be found here.

Signature

export declare function setCartBillingAddress<Res extends PartialUpdateCartResponse = UpdateCartResponse>(
	params: SetCartBillingAddressParams,
	options?: MethodOptions<CustomQuery<"updateCart">>
): Promise<Res>;

Parameters

NameRequiredTypeDescription
paramsRequiredSetCartBillingAddressParamsParameter object which can be used with this method. Refer to its type definition to learn about possible properties.
optionsOptionalMethodOptions<CustomQuery<"updateCart">>Options that can be passed to additionally configure the request or customize the logic in a plugin.

Returns

Returns a representation of the UpdateCartResponse.

Referenced Types

Examples

Setting the billing address for a cart.

import { sdk } from '~/sdk.config.ts';

const params = {
  cartId: 'abc123',
  cartVersion: 1,
  address: {
    // address properties
  }
};

const { cart } = await sdk.commerce.setCartBillingAddress(params);

Creating a custom GraphQL query for the method in middleware.config.js.

module.exports = {
  integrations: {
    ct: {
      // ...
      customQueries: {
        'set-cart-billing-address-custom-query': ({ query, variables, metadata }) => ({
           variables,
           query: `
             mutation updateCart(
               $id: String!,
                $version: Long!,
                $actions: [MyCartUpdateAction!]!,
                $locale: Locale!,
                $acceptLanguage: [Locale!],
                $currency: Currency!,
                $storeKey: KeyReferenceInput
              ) {
                cart: updateMyCart(id: $id, version: $version, actions: $actions, storeKey: $storeKey) {
                  ${metadata.fields}
                }
              }
           `;
        })
      }
    }
  }
};

Setting the billing address for a cart and using the customQuery parameter to leverage the custom GraphQL query from the previous example. Notice how we are using the type parameter to overwrite the default response interface.

import { sdk } from '~/sdk.config.ts';

type CustomUpdateCartResponse = {
  updateCart: {
   id: string;
   billingAddress: {
     id: string;
     streetName: string;
     city: string;
     country: string;
     state: string;
    }
  }
}

const params = {
  cartId: 'abc123',
  cartVersion: 1,
  address: {
    streetName: '123 Main St',
    city: 'Los Angeles',
    country: 'US',
    state: 'CA'
  }
};

const { cart } = await sdk.commerce.setCartBillingAddress<CustomUpdateCartResponse>(params, {
  customQuery: {
    updateCart: 'set-cart-billing-address-custom-query',
    metadata: {
       fields: 'id, billingAddress { id,streetName,city,country,state }'
    }
  }
});

On this page