Obtener el número total de productos
Cuenta los productos del proyecto. Pasa un cuerpo de filtro para contar solo los productos que cumplen determinadas condiciones.
Qué hace este ejemplo:
- Obtiene el número total de productos
- Opcionalmente filtra por condiciones de atributos
- Usa el número de productos para la paginación
Requisitos previos:
- Un
PROJECT_URLy unAPP_TOKENválidos para la API de OneEntry - Al menos un producto en el proyecto
Notas:
- Por defecto (
isShell: true), el SDK devuelve un objeto de error en lugar de lanzar una excepción, así que comprueba el resultado, p. ej.,'statusCode' in result. ConisShell: falselanza la excepción y los errores se manejan con try/catch. Consulta Errors. - Pasa un array vacío (
[]) para contar todos los productos.
Documentación de la plataforma:
Referencia del SDK:
Pruébalo en vivo
Ejecuta este ejemplo en el sandbox del SDK de JS. Conecta tu Project URL y App Token en la primera visita y luego abre:
Escenario
1. Importar defineOneEntry y definir PROJECT_URL y APP_TOKEN
import { defineOneEntry } from 'oneentry';
import type { IFilterParams } from 'oneentry';
const PROJECT_URL = 'https://your-project.oneentry.cloud';
const APP_TOKEN = 'your-app-token';
2. Crear un cliente de API con defineOneEntry()
const { Products } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. Definir, si se desea, un body de filtro (array vacío = todos los productos)
// To filter, use conditions like: [{ attributeMarker: 'price', conditionMarker: 'mth', conditionValue: 100 }]
const body: IFilterParams[] = [];
4. Obtener el recuento con Products.getProductsCount()
const data = await Products.getProductsCount(body);
if ('statusCode' in data) {
throw new Error(data.message);
}
console.log('Count data: ', data);
Resultado:
{
"totalAll": 12,
"totalInCategory": 0,
"totalInCategoryWithNested": 0
}
Ejemplo final
// 1. Import defineOneEntry and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';
import type { IFilterParams } from 'oneentry';
const PROJECT_URL = 'https://your-project.oneentry.cloud';
const APP_TOKEN = 'your-app-token';
// 2. Create an API client with defineOneEntry()
const { Products } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Optionally define a filter body (empty array = all products)
// To filter, use conditions like: [{ attributeMarker: 'price', conditionMarker: 'mth', conditionValue: 100 }]
const body: IFilterParams[] = [];
// 4. Get the count with Products.getProductsCount()
const data = await Products.getProductsCount(body);
if ('statusCode' in data) {
throw new Error(data.message);
}
console.log('Count data: ', data);