getProductsSharedStateKit
info
Learn more about the StateKit guide for working with reactive state management.
Retrieves a reactive state kit object that provides access to products query and service methods with Observable streams. This allows you to subscribe to product changes and load products on demand.
sdk.products.methods.getProductsSharedStateKit()getProductsSharedStateKit(): Promise<ErrorResult | SuccessResult<ProductsSharedStateKit>>Returns
Promise<ErrorResult | SuccessResult<ProductsSharedStateKit>>Promise that resolves to a success result with a state kit object containing query and service methods
Usage example
Ensure the SDK and the Products module are ready.
function onSdkReady() {
const moduleName = window.gw.sdk.common.constants.modules.PRODUCTS;
window.gw.sdk.runIfModuleReady(moduleName, () => {
window.gw.sdk.products.methods.getProductsSharedStateKit()
.then((response) => {
const stateKit = response.result;
// Load a product and subscribe to its changes
stateKit.service.addProductForFetching(123).subscribe((product) => {
console.log('Product loaded:', product);
});
// Subscribe to all products in store
stateKit.query.selectAll().subscribe((products) => {
console.log('All products:', products);
});
// Subscribe to product and variant together
stateKit.query.selectProductAndVariant(123, 456).subscribe(({ product, variant }) => {
if (product && variant) {
console.log('Product:', product.title);
console.log('Price:', variant.price);
}
});
})
.catch(console.error);
});
}
if (window.gw && window.gw.sdk) {
onSdkReady();
} else {
document.addEventListener("gwSdkReady", onSdkReady);
}
Types
ProductsSharedStateKit
ProductsSharedStateKitProperties
readonly queryProductsSharedStateKitQueryWrapperreadonly serviceProductsSharedStateKitServiceWrapper
ProductsSharedStateKitQueryWrapper
ProductsSharedStateKitQueryWrapperMethods
selectAll()Observable<ProductSdkDto[]>selectFirstVariantOfProduct(productId)Observable<ProductVariantSdkDto | null>selectProduct(productId)Observable<ProductSdkDto | null>selectProductAndVariant(productId, variantId)Observable<{ product?: ProductSdkDto; variant?: ProductVariantSdkDto }>selectProductIds()Observable<number[]>selectProductStatus(productId)Observable<LOADING | READY | "IN_QUEUE" | undefined>
Returns:
Parameters
| Name | Type | Description |
|---|---|---|
| productId | number | — |
Returns:
Observable<ProductVariantSdkDto | null>
Parameters
| Name | Type | Description |
|---|---|---|
| productId | number | — |
Returns:
Observable<ProductSdkDto | null>
Parameters
| Name | Type | Description |
|---|---|---|
| productId | number | — |
| variantId | number | null | — |
Returns:
Observable<{
product?: ProductSdkDto;
variant?: ProductVariantSdkDto
}>
Returns:
Observable<number[]>
Parameters
| Name | Type | Description |
|---|---|---|
| productId | number | — |
Returns:
Observable<LOADING | READY | "IN_QUEUE" | undefined>
ProductsSharedStateKitServiceWrapper
ProductsSharedStateKitServiceWrapperMethods
addProductForFetching(productId)Observable<ProductSdkDto>- Adds a product to the fetching queue and returns an Observable that emits the product once it's loaded.
addProductWithQuantityRulesForFetching(productId)void- Adds a product with quantity rules to the fetching queue.
Parameters
| Name | Type | Description |
|---|---|---|
| productId | number | ID of the product to fetch |
Parameters
| Name | Type | Description |
|---|---|---|
| productId | number | ID of the product to fetch with quantity rules |
ProductSdkDto
ProductSdkDtoProperties
handlestringidnumberimagesProductImageSdkDto[]titlestring
ProductVariantSdkDto
ProductVariantSdkDtoProperties
compareAtPriceProductPriceSdkDto | nullcurrentlyNotInStockbooleanidnumberimageProductImageSdkDto | nullpriceProductPriceSdkDtoquantityAvailablenumbertitlestring