Skip to content

selectSelectedPayment$(): Observable<BasketPayment | undefined>

The selectSelectedPayment$ method provides an observable stream that emits events whenever the selected payment option changes, allowing you to react to these changes in real-time.

Returned value

A returned value has a type of Observable<BasketPayment | undefined> where BasketPayment represents the BasketPayment model, which emits the currently selected payment method and updates whenever the selection changes.

Example

In this example, we subscribe to the selectSelectedPayment$ observable to monitor changes to the selected payment option.

useStorefront(async ({ eventBus, getApi }) => {
    eventBus.on('basket.initialized', async () => {
        const basketPaymentsApi = await getApi('basketPaymentsApi');

        const selectedPayment$ = basketPaymentsApi.selectSelectedPayment$();

        selectedPayment$.subscribe((selectedPayment) => {
            if (selectedPayment) {
                console.log('Payment option selected:', selectedPayment);
            } else {
                console.log('No payment option is currently selected.');
            }
        });
    });
});

Basket Payments API methods reference

Models reference