useUserCarts()

The useUserCarts() composable method allows for fetching and storing a list of the current user carts (both active and saved). Contrary to other cart-related composables, it does not accept a unique id as a parameter and only has a single instance.

carts

Main data object populated by the load() method.

Type

const carts: ComputedProperty<Cart[]>

References: Cart

loading

Indicates whether any of the composable methods is in progress.

Type

const loading: ComputedProperty<UseUserCartsLoading>;

References: UseUserCartsLoading

error

Contains errors thrown by any of the composable methods.

Type

const error: ComputedRef<UseUserCartsError>;

References: UseUserCartsError

load()

Fetches user carts and populates the carts property. Under the hood, it sends a request to the getCarts API endpoint.

Remember to log in first!

This method will not work when used during an anonymous session.

Type

interface LoadUserCartsProps extends BaseProps {
  savedCartsOnly?: boolean;
  pageSize?: number;
  currentPage?: number;
  sort?: string
}

async function load: (props?: LoadUserCartsProps) => Promise<void>

References: LoadUserCartsProps, BaseProps

Example

In the following example we are fetching a list of user's saved carts only (skipping the active cart), limit the number of returned Cart entries to five and select the BASIC subset of fields.

import { onMounted } from '@nuxtjs/composition-api';
import { useUserCarts } from '@vsf-enterprise/sapcc';

export default {
  setup() {
    const { carts, load: loadUserCarts } = useUserCarts();

    onMounted(async () => {
      await loadUserCarts({
        savedCartsOnly: true,
        pageSize: 5,
        fields: 'BASIC'
      });
    });

    return { carts };
  }
};