Lấy một sản phẩm đơn lẻ theo ID
Phương thức này cho phép bạn truy xuất một sản phẩm đơn lẻ theo ID số của nó, trả về đối tượng sản phẩm đầy đủ bao gồm các thuộc tính, giá cả và nội dung địa phương hóa.
✅ Mục đích của kịch bản:
- Lấy một sản phẩm đơn lẻ theo ID số của nó
- Truy cập giá sản phẩm và tiêu đề địa phương hóa
- Đọc các giá trị thuộc tính như tiền tệ từ sản phẩm
✅ Những gì bạn cần:
- Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với OneEntry API.
- Một ID sản phẩm số đã biết
📌 Quan trọng:
- 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 một 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 một sản phẩm đơn lẻ theo ID — Phương thức này cho phép bạn truy xuất một sản phẩm đơn lẻ theo ID số của nó, trả về đối tượng sản phẩm đầy đủ bao gồm các thuộc tính, giá cả và nội dung địa phương hóa.
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 { IAttributeValues } from 'oneentry/dist/base/utils';
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.getProductById() với phương thức getProductById()
Ví dụ:
const product = await Products.getProductById(15, 'en_US');
if ('statusCode' in product) {
throw new Error(product.message);
}
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": {}
}
4. Truy cập các trường sản phẩm
Ví dụ:
const title = product.localizeInfos?.title;
const price = product.price;
const attrs = product.attributeValues as IAttributeValues;
const currency = attrs.currency?.value;
console.log('Product: ', product);
console.log('Title: ', title);
console.log('Price: ', price);
console.log('Currency: ', currency);
Kết quả:
{
"product": {
"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": {}
},
"title": "Orange ball",
"price": 400,
"currency": "USD"
}
Ví dụ cuối cùng
// 1. Import defineOneEntry from SDK and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';
import { IAttributeValues } from 'oneentry/dist/base/utils';
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.getProductById() with [getProductById() method](/docs/products/#getProductById)
const product = await Products.getProductById(15, 'en_US');
if ('statusCode' in product) {
throw new Error(product.message);
}
// 4. Access product fields
const title = product.localizeInfos?.title;
const price = product.price;
const attrs = product.attributeValues as IAttributeValues;
const currency = attrs.currency?.value;
console.log('Product: ', product);
console.log('Title: ', title);
console.log('Price: ', price);
console.log('Currency: ', currency);