Alokai

addConfigurableProductsToCart

Add configurable products to cart.

Signature

declare function addConfigurableProductsToCart(
	context: Context,
	input: AddConfigurableProductsToCartInput,
	customQuery?: CustomQuery,
	customHeaders?: CustomHeaders
): Promise<FetchResult<AddConfigurableProductsToCartMutation>>;

Parameters

NameRequiredTypeDescription
contextRequiredContext
inputRequiredAddConfigurableProductsToCartInput
customQueryOptionalCustomQuery
customHeadersOptionalCustomHeaders

Referenced Types

Examples

Adding configurable products to cart with default parameters.

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

const cart = await sdk.magento.addConfigurableProductsToCart(
  {
    cart_id: '123',
    cart_items: [
      {
       data: {
         quantity: 1,
         sku: 'MH01-XS-Black',
       },
       parent_sku: 'MH01',
       customizable_options: [],
      }
    ]
  }
);

Creating a custom GraphQL query for adding configurable products to cart

module.exports = {
  integrations: {
    magento: {
      customQueries: {
        'add-configurable-products-to-cart-custom-query': ({ variables, metadata }) => ({
           variables,
           query: `
             mutation addConfigurableProductsToCart($input: AddConfigurableProductsToCartInput) {
               addConfigurableProductsToCart(input: $input) {
                 cart {
                   ${metadata.fields}
                 }
               }
             }`,
        }),
      },
    }
  }
};

Using a custom GraphQL query to modify response containing the cart, which is sent as part of the adding product to cart mutation

import { sdk } from '~/sdk.config.ts';
// reduce the amount of fields returned by the query, when compared to the default query
const customQuery = {
  cart: 'add-configurable-products-to-cart-custom-query',
  metadata: {
    fields: 'id items { uid }'
  }
};


const cart = await sdk.magento.addConfigurableProductsToCart(
  {
    cart_id: '123',
    cart_items: [
      {
       data: {
         quantity: 1,
         sku: 'MH01-XS-Black',
       },
       parent_sku: 'MH01',
       customizable_options: [],
      }
    ]
  },
  customQuery
);

// Result will contain only the fields specified in the custom query.

On this page