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

setShippingMethod

Method setting cart shipping method.

This method sends a POST request to the updateCart endpoint of the Vue Storefront API Middleware and passes the SetShippingMethod action to it. In turn, it sets a shipping method on cart in commercetools by sending a request to its GraphQL API. The default GraphQL query used by this method can be found here.

When the method is called with an incorrect cartVersion and commercetools API responds with the ConcurrentModification error, our Server Middleware automatically retries the failed request with the correct version. To disable that behavior, set the versionFallback to false.

In case you need to perform a more complex cart update with multiple update actions at once, use the updateCart method instead.

This method requires an ID of an existing shipping method passed as a parameter and can only be called on a cart with a shipping address set. Therefore, you might also want to see the getShippingMethods and setCartShippingAddress methods.

Signature

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

Parameters

NameRequiredTypeDescription
paramsRequiredSetShippingMethodParamsParameter 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 cart shipping method.

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

const { cart } = await sdk.commerce.setShippingMethod({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  shippingMethodId: '7015a612-a16b-47ab-9672-bc72fcfb129a'
});

Creating a custom GraphQL query for the method in middleware.config.js. We can use it to specify response fields as well as overwrite certain query variables.

module.exports = {
  integrations: {
    ct: {
      // ...
      customQueries: {
        'set-shipping-method-custom-query': ({ query, variables, metadata }) => ({
          variables: Object.assign(variables, metadata?.variables),
          query: `
            mutation updateCart(
              $id: String!,
              $version: Long!,
              $actions: [MyCartUpdateAction!]!,
              $storeKey: KeyReferenceInput
            ) {
              cart: updateMyCart(id: $id, version: $version, actions: $actions, storeKey: $storeKey) {
                ${metadata.fields}
              }
            }
          `;
        }
      }
    }
  }
};

Setting cart shipping method and selecting response fields using 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 = {
  cart: {
    id: string;
    version: string;
  }
}

const { cart } = await sdk.commerce.setShippingMethod<CustomUpdateCartResponse>({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  shippingMethodId: '7015a612-a16b-47ab-9672-bc72fcfb129a'
}, {
  customQuery: {
    updateCart: 'set-shipping-method-custom-query',
    metadata: {
      fields: 'id, version'
    }
  }
})

On this page