Lấy sản phẩm theo URL trang
Trong ví dụ này, chúng tôi sẽ trình bày cách lấy tất cả sản phẩm thuộc về một trang danh mục cụ thể bằng cách sử dụng API OneEntry.
✅ Mục đích của kịch bản:
- Kết nối với API OneEntry
- Lấy sản phẩm được lọc theo URL trang danh mục của chúng
- Xem danh sách sản phẩm đã trả về
✅ 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.
- Một trang danh mục được cấu hình trong OneEntry với một URL đã biết (ví dụ: "products").
- Các sản phẩm được gán cho trang danh mục đó.
📌 Quan trọng:
- Trả về
{ items, total }— không phải là một mảng đơn giản. - Tham số url phải khớp với pageUrl của một trang loại catalog_page.
- 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 sản phẩm theo URL trang — Trong ví dụ này, chúng tôi sẽ trình bày cách lấy tất cả sản phẩm thuộc về một trang danh mục cụ thể bằng cách sử dụng 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 { IProductsQuery } 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. Định nghĩa các tham số yêu cầu
Ví dụ:
const userQuery: IProductsQuery = {
offset: 0,
limit: 20,
sortKey: 'price',
sortOrder: 'ASC',
};
4. Lấy sản phẩm theo URL trang với Products.getProductsByPageUrl()
Ví dụ:
const data = await Products.getProductsByPageUrl('products', [], 'en_US', userQuery);
if ('statusCode' in data) {
throw new Error(data.message);
}
console.log('Products: ', data.items);
console.log('Total: ', data.total);
Kết quả:
{
"items": [
{
"id": 13,
"attributeSetIdentifier": "product",
"localizeInfos": {
"title": "Red ball"
},
"statusLocalizeInfos": {},
"isVisible": true,
"statusIdentifier": null,
"position": 1,
"templateIdentifier": null,
"shortDescTemplateIdentifier": null,
"price": 300,
"additional": {
"prices": {
"min": 100,
"max": 999
}
},
"sku": null,
"isSync": true,
"categories": [
"products"
],
"paymentStages": null,
"rating": {},
"isPositionLocked": false,
"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
}
},
"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": {},
"isPositionLocked": false,
"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
}
},
"moduleFormConfigs": [],
"discountConfig": {}
},
{
"id": 15,
"attributeSetIdentifier": "product",
"localizeInfos": {
"title": "Orange ball"
},
"statusLocalizeInfos": {},
"isVisible": true,
"statusIdentifier": null,
"position": 3,
"templateIdentifier": null,
"shortDescTemplateIdentifier": null,
"price": 400,
"additional": {
"prices": {
"min": 100,
"max": 999
}
},
"sku": null,
"isSync": true,
"categories": [
"products"
],
"paymentStages": null,
"rating": {},
"isPositionLocked": false,
"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
}
},
"moduleFormConfigs": [],
"discountConfig": {}
}
],
"total": 3
}
Ví dụ cuối cùng
// 1. Import defineOneEntry from SDK and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';
import type { IProductsQuery } 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. Define request parameters
const userQuery: IProductsQuery = {
offset: 0,
limit: 20,
sortKey: 'price',
sortOrder: 'ASC',
};
// 4. Get products by page URL with [Products.getProductsByPageUrl()](/docs/products/getProductsByPageUrl)
const data = await Products.getProductsByPageUrl('products', [], 'en_US', userQuery);
if ('statusCode' in data) {
throw new Error(data.message);
}
console.log('Products: ', data.items);
console.log('Total: ', data.total);