getProductVariant(productId: number, options: TProductVariantOptions): Promise<ProductStock | null>¶
The getProductVariant is an asynchronous method that allows to fetch a single product variant by its id and selected options.
Input parameters¶
productId - a number representing the product id.
options - an object with information about stock we want to get. You can read more about TProductVariantOptions object here.
Returned value¶
A returned value has a type of Promise<ProductStock | null>. You can read more about ProductStock model here.
Example¶
In this example we make a ProductFetcherApi call to get a product variant with id 1 and selected options.
useStorefront(async (storefront) => {
let productFetcherApi = await storefront.getApi('ProductFetcherApi');
if (!productFetcherApi) {
const featureSystemApi = this.getApiSync('FeatureSystemApi');
await featureSystemApi.registerDynamic('ProductFetcher');
productFetcherApi = await storefront.getApi('ProductFetcherApi');
}
const productVariant = await productFetcherApi.getProductVariant(1, { variantOptions: { 25: '1' }});
console.log(productVariant);
});