setBillingAddress(action: BasketAction, address: TBasketAddressDTO): Promise<Record<string, string[]> | undefined>¶
The setBillingAddress
is an asynchronous method allowing to change a value of a billing address in a basket. It is recommended to use this method while updating a value of an address because of convenient validation provided by getSectionValidation and selectSectionValidation methods.
Input parameters¶
action¶
action
is a mandatory parameter of the BasketAction type that represents the action that we want to perform with a billing address.
address¶
address
is a mandatory parameter of the TBasketAddressDTO type which represents a new value of the billing address.
Returned value¶
A returned value has a type of Promise<Record<string, string[]> | undefined>
with the field ids as keys and their potential errors and messages they may have occurred during the change process as their values.
Event Bus events¶
This API method dispatches the following events with the Event Bus:
- basket.basketUpdated - after the address has been successfully changed
- FlashMessengerApi.addFlashMessages - when an error occurs while setting an address
- basket.unexpectedError - when an error occurs while setting an address
Example¶
In this example we make a basketAddressesApi
call to set a new value of a billing address. We also use a getActions method from Basket Overall API to get the action for changing the billing address.
useStorefront(async (storefront) => {
let basketAddressesApi = storefront.getApiSync('basketAddressesApi');
let basketOverallApi = storefront.getApiSync('basketOverallApi');
if (!basketAddressesApi || !basketOverallApi) {
const featureSystemApi = this.getApiSync('FeatureSystemApi');
await featureSystemApi.registerDynamic('basket');
basketAddressesApi = storefront.getApiSync('basketAddressesApi');
basketOverallApi = storefront.getApiSync('basketOverallApi');
}
const changeBillingAddressAction = basketOverallApi.getActions().changeBillingAddress;
await basketAddressesApi.setBillingAddress(changeBillingAddressAction, {
isCompany: false,
companyName: '',
firstName: 'John',
lastName: 'Snow',
personalIdNumber: '',
taxId: '',
countryId: 'US',
city: 'My City',
postalCode: '12-345',
street: 'example street 8',
street2: '',
phone: '123456789',
state: '',
useAsBillingAddress: false,
createUserAccount: false
});
});
Basket Address API methods reference¶
- getShippingAddress
- selectShippingAddress$
- setShippingAddress
- getBillingAddress
- selectBillingAddress$
- isBillingAddressEqualShippingAddress
- selectIsBillingAddressEqualShippingAddress$
- isDifferentShippingAddressRequired
- selectIsDifferentShippingAddressRequired$
- setBillingAddressAsShippingAddress
- getSectionValidation
- selectSectionValidation$