Chuyển đến nội dung chính

Lấy sản phẩm theo ID

Trong ví dụ này, chúng tôi sẽ trình bày cách lấy nhiều sản phẩm cùng một lúc bằng cách truyền một danh sách ID sản phẩm phân tách bằng dấu phẩy thông qua API OneEntry.

✅ Mục đích của kịch bản:

  • Kết nối với API OneEntry
  • Truyền một danh sách ID sản phẩm phân tách bằng dấu phẩy
  • Lấy các sản phẩm phù hợp và xem xét kết quả

✅ Những gì bạn cần:

  • Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với API OneEntry.
  • Các ID sản phẩm đã biết tồn tại trong dự án OneEntry của bạn.

📌 Quan trọng:

  • getProductsByIds trả về một mảng đơn giản IProductsEntity[], KHÔNG phải là { items, total }.
  • Những ví dụ này không bao gồm xử lý lỗi.
  • Bạn có thể quản lý lỗi bằng cách sử dụng khối try-catch hoặc bằng cách sử dụng cấu trúc như await Promise.catch((error) => error).

📚 Xem trong tài liệu:

📦 Tham khảo SDK:

Thử nghiệm trực tiếp

Chạy phương thức này một cách tương tác trong JS SDK sandbox — kết nối URL Dự án và Mã thông báo Ứng dụng của bạn khi lần đầu truy cập, sau đó mở:

  • Lấy sản phẩm theo ID — Trong ví dụ này, chúng tôi sẽ trình bày cách lấy nhiều sản phẩm cùng một lúc bằng cách truyền một danh sách ID sản phẩm phân tách bằng dấu phẩy thông qua API OneEntry.

Kịch bản

1. Nhập defineOneEntry từ SDK và định nghĩa PROJECT_URL và APP_TOKEN

Ví dụ:

import { defineOneEntry } from 'oneentry';
import type { IProductsEntity } from 'oneentry/dist/products/productsInterfaces';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

2. Tạo một khách hàng API với defineOneEntry()

Ví dụ:

const { Products } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Gọi Products.getProductsByIds() với Products.getProductsByIds()

Ví dụ:

const products = await Products.getProductsByIds('13,14,15', 'en_US');
console.log('Products: ', products);
Kết quả:
[
{
"id": 15,
"attributeSetIdentifier": "product",
"localizeInfos": {
"title": "Orange ball"
},
"statusLocalizeInfos": {},
"isVisible": true,
"statusIdentifier": null,
"position": 1,
"templateIdentifier": null,
"shortDescTemplateIdentifier": null,
"price": 400,
"additional": {
"prices": {
"min": 100,
"max": 999
}
},
"sku": null,
"isSync": true,
"categories": [
"products"
],
"paymentStages": null,
"rating": {},
"attributeValues": {
"price": {
"type": "real",
"value": "400",
"isIcon": false,
"position": 0,
"additionalFields": {},
"isProductPreview": false
},
"currency": {
"type": "string",
"value": "USD",
"isIcon": false,
"position": 1,
"additionalFields": {},
"isProductPreview": false
},
"image": {
"type": "image",
"value": [],
"isIcon": false,
"position": 2,
"additionalFields": {},
"isProductPreview": true
}
},
"productPages": [
{
"id": 17,
"pageId": 49,
"productId": 15,
"positionId": 1287,
"categoryPath": "products"
}
],
"blocks": [],
"moduleFormConfigs": [],
"discountConfig": {}
},
{
"id": 14,
"attributeSetIdentifier": "product",
"localizeInfos": {
"title": "Green ball"
},
"statusLocalizeInfos": {},
"isVisible": true,
"statusIdentifier": null,
"position": 2,
"templateIdentifier": null,
"shortDescTemplateIdentifier": null,
"price": 340,
"additional": {
"prices": {
"min": 100,
"max": 999
}
},
"sku": null,
"isSync": true,
"categories": [
"products"
],
"paymentStages": null,
"rating": {},
"attributeValues": {
"price": {
"type": "real",
"value": "340",
"isIcon": false,
"position": 0,
"additionalFields": {},
"isProductPreview": false
},
"currency": {
"type": "string",
"value": "USD",
"isIcon": false,
"position": 1,
"additionalFields": {},
"isProductPreview": false
},
"image": {
"type": "image",
"value": [],
"isIcon": false,
"position": 2,
"additionalFields": {},
"isProductPreview": true
}
},
"productPages": [
{
"id": 18,
"pageId": 49,
"productId": 14,
"positionId": 1288,
"categoryPath": "products"
}
],
"blocks": [],
"moduleFormConfigs": [],
"discountConfig": {}
},
{
"id": 13,
"attributeSetIdentifier": "product",
"localizeInfos": {
"title": "Red ball"
},
"statusLocalizeInfos": {},
"isVisible": true,
"statusIdentifier": null,
"position": 3,
"templateIdentifier": null,
"shortDescTemplateIdentifier": null,
"price": 300,
"additional": {
"prices": {
"min": 100,
"max": 999
}
},
"sku": null,
"isSync": true,
"categories": [
"products"
],
"paymentStages": null,
"rating": {},
"attributeValues": {
"price": {
"type": "real",
"value": "300",
"isIcon": false,
"position": 0,
"additionalFields": {},
"isProductPreview": false
},
"currency": {
"type": "string",
"value": "USD",
"isIcon": false,
"position": 1,
"additionalFields": {},
"isProductPreview": false
},
"image": {
"type": "image",
"value": [],
"isIcon": false,
"position": 2,
"additionalFields": {},
"isProductPreview": true
}
},
"productPages": [
{
"id": 19,
"pageId": 49,
"productId": 13,
"positionId": 1289,
"categoryPath": "products"
}
],
"blocks": [],
"moduleFormConfigs": [],
"discountConfig": {}
}
]

4. Trả về một mảng đơn giản, KHÔNG phải là { items, total }

Ví dụ:

const titles = (products as IProductsEntity[]).map((p) => p.localizeInfos?.title);
console.log('titles: ', titles);
Kết quả:
[
"Orange ball",
"Green ball",
"Red ball"
]

Ví dụ cuối cùng

// 1. Import defineOneEntry from SDK and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';
import type { IProductsEntity } from 'oneentry/dist/products/productsInterfaces';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

// 2. Creating an API client with [defineOneEntry()](/docs/index/#Installation)
const { Products } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Call Products.getProductsByIds() with [Products.getProductsByIds()](/docs/products/getProductsByIds)
const products = await Products.getProductsByIds('13,14,15', 'en_US');
console.log('Products: ', products);

// 4. Returns a plain array, NOT `{ items, total }`
const titles = (products as IProductsEntity[]).map((p) => p.localizeInfos?.title);
console.log('titles: ', titles);