useProduct composable
useProduct
composable allows loading product details or list with params for sorting, filtering and pagination.
API
useProduct
composable returns the following properties:
getProductList
- function that fetches a list of products with sorting, filtering and pagination.getProductDetails
- function that fetches a product details with sorting, filtering and pagination.getProductPath
- function that gets a product path from url_rewrites or url_key .error
- ref that contains an errors from the composable methods.loading
- ref that contains information whether any of the composable methods is loading.
Interfaces
type GetProductSearchParams = {
search?: string;
filter?: ProductAttributeFilterInput;
pageSize?: number;
currentPage?: number;
sort?: ProductAttributeSortInput;
configurations?: string[];
};
interface UseProductErrors {
getProductList: Error | null;
getProductDetails: Error | null;
}
interface UseProductInterface {
error: DeepReadonly<Ref<UseProductErrors>>;
loading: Readonly<Ref<boolean>>;
getProductList(searchParams: GetProductSearchParams): Promise<ProductList | null>;
getProductDetails(searchParams: GetProductSearchParams): Promise<ProductDetails | null>;
getProductPath(product: Product): string;
}
productGetters
productGetters
is a set of helper functions that can be used to get data from the product. They receive product object as a parameter and return the data from it.
product parameter
getName
- returns the name of the productgetSlug
- returns the slug of the productgetPrice
- returns an object with the prices of the product:regular
- returns the regular price of the productspecial
- returns the special price of the productmaximum
- returns the maximum price of the productfinal
- returns final price with discounts, etc.
getGallery
- returns an object with the media content:small
- returns URL for small sized imagenormal
- returns URL for normal sized imagebig
- returns URL for big sized image
getCoverImage
- returns the main product cover image URLgetAttributes
- returns an object with the attributes objects, can be filter by attribute namegetDescription
- returns product descriptiongetCategoryIds
- returns an array of product category IDsgetId
- returns product IDgetTotalReviews
- returns a number of user's reviews for a productgetAverageRating
- returns a number of an average product's ratinggetBreadcrumbs
- returns an array of breadcrumb objects:text
- text of the breadcrumblink
- link of the breadcrumb
getCategory
- returns product's category objectgetProductRelatedProduct
- returns a related productgetProductSku
- returns SKU of the productgetProductThumbnailImage
- returns thumbnail image URLgetProductUpsellProduct
- returns the upsell productgetShortDescription
- returns a short product's descriptiongetTypeId
- returns type idgetSwatchData
- returns product's swatch datagetGroupedProducts
- returns grouped productsgetBundleProducts
- returns bundle products
productGetters usage
import productGetters from '~/modules/catalog/product/getters/productGetters';
const productName = productGetters.getName(product);
const productSlug = productGetters.getSlug(product);
const productDescription = productGetters.getDescription(product);
Examples
Search products
Handle user search input and fetch list of products:
<script>
import { useProduct } from '~/composables';
setup() {
const term = ref('');
const { getProductList } = useProduct();
const rawHandleSearch = async (searchTerm: string) => {
term.value = searchTerm;
const productList : Products = await getProductList({
pageSize: 30,
search: term.value,
}) as Products;
};
}
</script>
Get product details
Fetch product details by sku
:
import { usePageStore } from '~/stores/page';
import { useProduct } from '~/composables';
setup() {
const { routeData } = usePageStore();
const { getProductDetails } = useProduct();
const getBaseSearchQuery = {
filter: {
sku: {
eq: routeData.sku,
},
}
};
const fetchProductBaseData = async (searchQuery = getBaseSearchQuery()) => {
const result = await getProductDetails({
...searchQuery,
});
product.value = result.items[0] as Product;
};
useFetch(async () => {
await fetchProductBaseData();
});
}
Get product path
Get product's path and pass as link prop to SfProductCard
component:
<template>
<div>
<SfProductCard
//...
:link="localePath(getProductPath(product))"
/>
</div>
</template>
<script lang="ts">
import { useProduct } from '~/composables';
setup() {
const { getProductPath } = useProduct();
return {
getProductPath,
};
}
</script>