Products
Introduction
This section will cover the basics of products and how to fetch them. We will also cover how to fetch the product's variants and options.
API Reference
You can find all available SAPCC module methods and their description in the API Reference.
Fetching a single product
To fetch a single product, you can use the getProduct method.
Fetching products by their id is the most common use case. To do so, you can pass the product's id as the id
parameter.
import { sdk } from '~/sdk';
const product = await sdk.sapcc.getProduct({ id: '1992695' });
You can specify the fields you want to receive in the response by passing them as the fields
parameter. By default, the possible values are BASIC
, DEFAULT
or FULL
, however, you can also pass a specific field name, such as price
.
import { sdk } from '~/sdk';
const product = await sdk.sapcc.getProduct({
id: '1992695',
fields: 'BASIC'
});
You can also specify the language and currency you want to use for the product's price. If the price and currency are not specified, the default values will be used.
const product = await sdk.sapcc.getProduct({
id: '1992695',
fields: 'BASIC',
lang: 'en',
currency: 'USD'
});
Fetching multiple products
To fetch multiple products, you can use the searchProduct method.
When calling the searchProduct
method without any parameters, it will return all products.
import { sdk } from '~/sdk';
const products = await sdk.sapcc.searchProduct();
Pagination
You can use the pageSize
and currentPage
parameters to paginate the results.
import { sdk } from '~/sdk';
const products = await sdk.sapcc.searchProduct({
pageSize: 10,
currentPage: 1
});
Filtering
You can use the filter
parameter to filter the results.
import { sdk } from '~/sdk';
const filteredProducts = await sdk.sapcc.searchProduct({
filters: {
allCategories: ['collections', 'brands'],
reviewAvgRating: [4.4]
}
});
Sorting
You can use the sort
parameter to sort the results.
import { sdk } from '~/sdk';
const result = await sdk.sapcc.searchProduct({
filters: {
allCategories: ['collections', 'brands'],
reviewAvgRating: [4.4]
},
sort: 'topRated'
});
Simple search
You can use the searchTerm
parameter to perform a simple search. The searchTerm
parameter accepts a string with the search term.
import { sdk } from '~/sdk';
const result = await sdk.sapcc.searchProduct({
searchTerm: 'sneakers',
fields: 'freeTextSearch'
});
Managing Product Reviews
To fetch product reviews, you can use the getProductReviews method.
import { sdk } from '~/sdk';
const product = await sdk.sapcc.getProductReviews({
productCode: '1992695'
});
You can specify the fields you want to receive in the response by passing them as the fields
parameter.
import { sdk } from '~/sdk';
const reviews = await sdk.sapcc.getProductReviews({
productCode: '1992695',
fields: 'BASIC'
});
To limit the number of reviews returned, you can use the maxCount
parameter.
import { sdk } from '~/sdk';
const reviews = await sdk.sapcc.getProductReviews({
productCode: '1992695',
maxCount: 4
});
To create a product review, you can use the createProductReview method.
const review = {
comment: 'This is a great product! Highly recommend it.',
rating: 5,
alias: 'Happy Customer'
};
const product = await sdk.sapcc.createProductReview({
productCode: '12345',
review
});