useCartPromotions(id?: string)
The useCartPromotions()
composable allows for:
- apllying vouchers on a cart
- removing vouchers from a cart
useCartPromotions()
should be treated as an extension for useCart()
. To bind particular instances of the useCart()
and useCartPromotions()
together, they simply have to be called with the same unique id
as the only argument.
Example
In the following example we are loading a new session cart using useCart().load
method and exposing additional functionalities (i.e. adding and removing vouchers) with the useCartPromotions()
composable.
import { onMounted } from '@nuxtjs/composition-api';
import { useCart, useCartPromotions } from '@vsf-enterprise/sapcc';
export default {
setup() {
const { cart, load } = useCart('unique-id');
const { addVoucher, removeVoucher } = useCartPromotions('unique-id');
onMounted(async () => {
await load();
});
return { addVoucher, removeVoucher };
}
}
Be careful about shared state
You should pass a unique id
as a parameter to avoid state conflicts when using multiple instances of this composable. Every instance with the same id
(or those that lack it) will share the same state, even on different pages.
loading
Indicates whether any of the composable methods is in progress.
Type
const loading: ComputedProperty<UseCartPromotionsLoading>;
References: UseCartPromotionsLoading
error
Contains errors thrown by any of the composable methods.
Type
const error: ComputedRef<UseCartPromotionsError>;
References: UseCartPromotionsError
addVoucher()
Applies a voucher on a cart and updates the cart object of the bound useCart()
composable. Under the hood, it calls the addVoucherAndGetNewCartVersion API endpoint.
Type
interface AddCartVoucherProps extends BaseProps {
voucherId: string;
}
async function addVoucher(props: AddCartVoucherProps): Promise<void>
References: AddCartVoucherProps, BaseProps
Example
In this example we are creating a simple wrapper around the addVoucher()
method. It accepts a raw Voucher object and calls the addVoucher()
method with the right arguments.
import { useCartPromotions } from '@vsf-enterprise/sapcc';
export default {
setup() {
const { addVoucher } = useCartPromotions('unique-id');
const addCartVoucher = async (voucher) => {
const { code } = voucher;
await addVoucher({ voucherId: code });
}
return { addCartVoucher };
}
}
removeVoucher()
Removes a voucher which had been aplied on a cart and updates the cart object of the bound useCart()
composable. Under the hood, it calls the removeVoucherFromCart API endpoint.
Type
interface RemoveCartVoucherProps extends BaseProps {
voucherId: string;
}
async function removeVoucher(props: RemoveCartVoucherProps): Promise<void>
References: RemoveCartVoucherProps, BaseProps
Example
In this example we are creating a simple wrapper around the removeVoucher()
method. It accepts a raw Voucher object and calls the removeVoucher()
method with the right arguments.
import { useCartPromotions } from '@vsf-enterprise/sapcc';
export default {
setup() {
const { removeVoucher } = useCartPromotions('unique-id');
const removeCartVoucher = async (voucher) => {
const { code } = voucher;
await removeVoucher({ voucherId: code });
}
return { removeCartVoucher };
}
}