Alokai

cartTotalQty

Get cart total quantity. This method is optimized to fetch only total quantity of the cart and not the whole cart object. Do not use cart query if you want to fetch only total quantity of the cart.

Signature

declare function cartTotalQty(
	context: Context,
	cartId: string,
	customHeaders?: CustomHeaders
): Promise<ApolloQueryResult<CartQuery>>;

Parameters

NameRequiredTypeDescription
contextRequiredContext
cartIdRequiredstring
customHeadersOptionalCustomHeaders

Referenced Types

Examples

Simple usage:

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

// fetch total quantity of the cart
const { data } = await sdk.magento.cartTotalQty('some_cart_id');

// total quantity of the cart available in data.cart.total_quantity

Creating a custom GraphQL query for cart total quantity

module.exports = {
  integrations: {
    magento: {
      customQueries: {
        'cart-total-qty-custom-query': ({ variables, metadata }) => ({
           variables,
           query: `
             query cart($cartId: String!) {
               cart(cart_id: $cartId) {
                 ${metadata.fields}
               }
             }`
        }),
      },
    }
  }
};

Using a custom GraphQL query to fetch additional cart data

import { sdk } from '~/sdk.config.ts';
// Include additional fields alongside total quantity
const customQuery = {
  cartTotalQty: 'cart-total-qty-custom-query',
  metadata: {
    fields: 'total_quantity id is_virtual'
  }
};

const { data } = await sdk.magento.cartTotalQty('some_cart_id', customQuery);

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

On this page