getProducts
Fetch filtered and paginated products.
In turn, it fetches a products from commercetools by sending a request to its GraphQL API.
By default, it uses the getProductDefaultQuery GraphQL query
Signature
declare function getProducts(
context: CommercetoolsIntegrationContext,
params: GetProductParams,
customQuery?: CustomQuery
): Promise<GetProductResponse>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
context | Required | CommercetoolsIntegrationContext | |
params | Required | GetProductParams | |
customQuery | Optional | CustomQuery |
Returns
Returns a representation of the GetProductResponse.
Referenced Types
Examples
Fetching the first page of all products.
const { products } = await sdk.commerce.getProducts();Fetching products the way, that the response is cacheable, by passing variables explicitly instead of getting values from cookies.
const { products } = await sdk.commerce.getProducts({
country: "PL",
currency: "USD",
customerGroupId: null,
channel: null,
locale: "en",
});Fetching the third page of all products sliced by 25 products.
const { products } = await sdk.commerce.getProducts({
offset: 50,
limit: 25
});Fetching the first page of products filtered by category identifier.
const { products } = await sdk.commerce.getProducts({
catId: '3d45e2d2-fe47-4e8b-968e-faa87991f233'
});Creating a custom GraphQL query for fetching product.
export const integrations = {
ct: {
// ...
customQueries: {
'get-product-custom-query': ({ variables, metadata }) => ({
variables,
query: `
query products(
$where: String
$sort: [String!]
$limit: Int
$offset: Int
$skus: [String!]
) {
products(
where: $where
sort: $sort
limit: $limit
offset: $offset
skus: $skus
) {
offset
count
total
results {
${metadata}
}
}
}
`
}),
}
}
}
};Fetching products and using the customQuery parameter to leverage the custom GraphQL query from the previous example.
const { products } = await sdk.commerce.getProducts(undefined, {
products: 'get-product-custom-query',
metadata: `
id
masterData {
current {
name(acceptLanguage: "en")
}
}
`
});