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

createMyOrderFromCart

Method creating an order from cart.

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

This method can only be called on a non-empty cart with a shipping address set. Therefore, you might also want to see the addProductToCart and setCartShippingAddress methods.

Signature

export declare function createMyOrderFromCart<Res extends PartialCreateMyOrderFromCartResponse = CreateMyOrderFromCartResponse>(
	params: CreateMyOrderFromCartParams,
	options?: MethodOptions<CustomQuery<"createMyOrderFromCart">>
): Promise<Res>;

Parameters

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

Returns

Returns a representation of the CreateMyOrderFromCartResponse.

Referenced Types

Examples

Creating an order from cart.

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

const { order } = await sdk.commerce.createMyOrderFromCart({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1
});

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: {
        'create-my-order-from-cart-custom-query': ({ query, variables, metadata }) => ({
          variables: Object.assign(variables, metadata?.variables),
          query: `
            mutation createMyOrderFromCart($draft: OrderMyCartCommand!, $storeKey: KeyReferenceInput) {
              order: createMyOrderFromCart(draft: $draft, storeKey: $storeKey) {
                ${metadata?.fields}
              }
            }
          `
        })
      }
    }
  }
};

Creating an order from cart 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 CustomCreateMyOrderFromCartResponse = {
  order: {
    id: string;
    version: string;
  }
}

const { order } = await sdk.commerce.createMyOrderFromCart<CustomCreateMyOrderFromCartResponse>({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1
}, {
  customQuery: {
    updateCart: 'create-my-order-from-cart-custom-query',
    metadata: {
      fields: 'id, version'
    }
  }
})

On this page