API Danh sách Danh mục
API này cho phép lấy danh sách danh mục cha, danh mục con với đầy đủ thông tin chi tiết, phân cấp và phân trang.
Ví dụ minh họa: Danh mục sản phẩm đa cấp
Ví dụ minh họa: Danh mục sản phẩm đa cấp
GET https://api.socdo.vn/v1/categories_list
Các tham số:
- type: Loại danh mục - 'all' (tất cả), 'parents' (chỉ cha), 'children' (chỉ con) - mặc định 'all'
- parent_id: ID danh mục cha (để lấy danh mục con của cha cụ thể)
- include_children: Bao gồm danh mục con (1/0) - mặc định 1
- include_products_count: Đếm số sản phẩm trong danh mục (1/0) - mặc định 0
- page: Trang hiện tại (mặc định 1)
- limit: Số lượng danh mục mỗi trang (mặc định 50, tối đa 200)
Ví dụ lấy tất cả danh mục bằng cURL:
curl -X GET "https://api.socdo.vn/v1/categories_list?type=all&include_children=1&page=1&limit=20" -H "Authorization: Bearer <token>"
Ví dụ lấy chỉ danh mục cha bằng cURL:
curl -X GET "https://api.socdo.vn/v1/categories_list?type=parents&include_children=1&include_products_count=1" -H "Authorization: Bearer <token>"
Ví dụ lấy danh mục con của cha cụ thể bằng cURL:
curl -X GET "https://api.socdo.vn/v1/categories_list?type=children&parent_id=5&include_products_count=1" -H "Authorization: Bearer <token>"
Ví dụ kết nối bằng PHP:
<?php
// Lấy tất cả danh mục với con
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.socdo.vn/v1/categories_list?" . http_build_query([
"type" => "all",
"include_children" => 1,
"include_products_count" => 1,
"page" => 1,
"limit" => 20
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer <token>"
]);
$response = curl_exec($ch);
curl_close($ch);
// Lấy danh mục cha
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://api.socdo.vn/v1/categories_list?" . http_build_query([
"type" => "parents",
"include_children" => 1,
"include_products_count" => 1
]));
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HTTPHEADER, [
"Authorization: Bearer <token>"
]);
$response2 = curl_exec($ch2);
curl_close($ch2);
echo $response;
?>
Dữ liệu trả về mẫu (Danh mục cha):
{
"success": true,
"message": "Lấy danh sách danh mục thành công",
"data": {
"categories": [
{
"id": 1,
"name": "Điện thoại & Phụ kiện",
"slug": "dien-thoai-phu-kien",
"parent_id": 0,
"order": 1,
"icon": "📱",
"image": "/uploads/minh-hoa/dienthoai.jpg",
"description": "Điện thoại di động và các phụ kiện liên quan",
"title": "Điện thoại & Phụ kiện - Socdo",
"meta_description": "Mua điện thoại và phụ kiện chính hãng tại Socdo",
"is_featured": 1,
"is_trending": 1,
"show_on_homepage": 1,
"status": 1,
"category_url": "https://api.socdo.vn/danh-muc/1/dien-thoai-phu-kien.html",
"icon_data": {
"type": "text",
"content": "📱"
},
"image_url": "https://api.socdo.vn/uploads/minh-hoa/dienthoai.jpg",
"thumb_url": "https://api.socdo.vn/uploads/thumbs/list_danhmuc_noibat/dienthoai.jpg",
"is_parent": true,
"level": 1,
"parent_info": null,
"products_count": 1250,
"children": [
{
"id": 11,
"name": "Điện thoại",
"slug": "dien-thoai",
"order": 1,
"url": "https://api.socdo.vn/danh-muc/11/dien-thoai.html"
},
{
"id": 12,
"name": "Phụ kiện điện thoại",
"slug": "phu-kien-dien-thoai",
"order": 2,
"url": "https://api.socdo.vn/danh-muc/12/phu-kien-dien-thoai.html"
},
{
"id": 13,
"name": "Tai nghe",
"slug": "tai-nghe",
"order": 3,
"url": "https://api.socdo.vn/danh-muc/13/tai-nghe.html"
}
],
"children_count": 3,
"badges": ["Nổi bật", "Xu hướng", "Hiển thị trang chủ"]
},
{
"id": 2,
"name": "Máy tính & Laptop",
"slug": "may-tinh-laptop",
"parent_id": 0,
"order": 2,
"icon": "/uploads/icons/laptop.png",
"image": "/uploads/minh-hoa/laptop.jpg",
"description": "Máy tính để bàn, laptop và phụ kiện",
"title": "Máy tính & Laptop - Socdo",
"meta_description": "Mua laptop và máy tính chính hãng giá tốt",
"is_featured": 1,
"is_trending": 0,
"show_on_homepage": 1,
"status": 1,
"category_url": "https://api.socdo.vn/danh-muc/2/may-tinh-laptop.html",
"icon_data": {
"type": "image",
"url": "https://api.socdo.vn/uploads/icons/laptop.png",
"alt": "Máy tính & Laptop"
},
"image_url": "https://api.socdo.vn/uploads/minh-hoa/laptop.jpg",
"thumb_url": "https://api.socdo.vn/uploads/thumbs/list_danhmuc_noibat/laptop.jpg",
"is_parent": true,
"level": 1,
"parent_info": null,
"products_count": 856,
"children": [
{
"id": 21,
"name": "Laptop",
"slug": "laptop",
"order": 1,
"url": "https://api.socdo.vn/danh-muc/21/laptop.html"
},
{
"id": 22,
"name": "Máy tính để bàn",
"slug": "may-tinh-de-ban",
"order": 2,
"url": "https://api.socdo.vn/danh-muc/22/may-tinh-de-ban.html"
}
],
"children_count": 2,
"badges": ["Nổi bật", "Hiển thị trang chủ"]
}
],
"pagination": {
"current_page": 1,
"total_pages": 3,
"total_records": 25,
"per_page": 20,
"has_next": true,
"has_prev": false
},
"filters": {
"type": "all",
"parent_id": 0,
"include_children": 1,
"include_products_count": 1
}
}
}
Dữ liệu trả về mẫu (Danh mục con):
{
"success": true,
"message": "Lấy danh sách danh mục thành công",
"data": {
"categories": [
{
"id": 11,
"name": "Điện thoại",
"slug": "dien-thoai",
"parent_id": 1,
"order": 1,
"icon": "📱",
"image": "/uploads/minh-hoa/dienthoai-main.jpg",
"description": "Điện thoại di động các hãng",
"title": "Điện thoại - Socdo",
"meta_description": "Điện thoại iPhone, Samsung, Xiaomi chính hãng",
"is_featured": 1,
"is_trending": 1,
"show_on_homepage": 0,
"status": 1,
"category_url": "https://api.socdo.vn/danh-muc/11/dien-thoai.html",
"icon_data": {
"type": "text",
"content": "📱"
},
"image_url": "https://api.socdo.vn/uploads/minh-hoa/dienthoai-main.jpg",
"thumb_url": "https://api.socdo.vn/uploads/thumbs/list_danhmuc_noibat/dienthoai-main.jpg",
"is_parent": false,
"level": 2,
"parent_info": {
"id": 1,
"name": "Điện thoại & Phụ kiện",
"slug": "dien-thoai-phu-kien",
"url": "https://api.socdo.vn/danh-muc/1/dien-thoai-phu-kien.html"
},
"products_count": 850,
"children": [],
"children_count": 0,
"badges": ["Nổi bật", "Xu hướng"]
},
{
"id": 12,
"name": "Phụ kiện điện thoại",
"slug": "phu-kien-dien-thoai",
"parent_id": 1,
"order": 2,
"icon": "🔌",
"image": "/uploads/minh-hoa/phukien.jpg",
"description": "Sạc, cáp, ốp lưng điện thoại",
"title": "Phụ kiện điện thoại - Socdo",
"meta_description": "Phụ kiện điện thoại chính hãng giá rẻ",
"is_featured": 0,
"is_trending": 0,
"show_on_homepage": 0,
"status": 1,
"category_url": "https://api.socdo.vn/danh-muc/12/phu-kien-dien-thoai.html",
"icon_data": {
"type": "text",
"content": "🔌"
},
"image_url": "https://api.socdo.vn/uploads/minh-hoa/phukien.jpg",
"thumb_url": "https://api.socdo.vn/uploads/thumbs/list_danhmuc_noibat/phukien.jpg",
"is_parent": false,
"level": 2,
"parent_info": {
"id": 1,
"name": "Điện thoại & Phụ kiện",
"slug": "dien-thoai-phu-kien",
"url": "https://api.socdo.vn/danh-muc/1/dien-thoai-phu-kien.html"
},
"products_count": 400,
"children": [],
"children_count": 0,
"badges": []
}
],
"pagination": {
"current_page": 1,
"total_pages": 1,
"total_records": 2,
"per_page": 50,
"has_next": false,
"has_prev": false
},
"filters": {
"type": "children",
"parent_id": 1,
"include_children": 1,
"include_products_count": 1
}
}
}
Tính năng chi tiết:
- Phân cấp danh mục: Hỗ trợ danh mục cha (level 1) và con (level 2)
- Lọc linh hoạt: Lấy tất cả, chỉ cha, chỉ con, hoặc con của cha cụ thể
- Thông tin đầy đủ: Icon, hình ảnh, mô tả, SEO meta
- Đếm sản phẩm: Tùy chọn đếm số sản phẩm trong mỗi danh mục
- Danh mục con: Tự động lấy danh mục con của danh mục cha
- URL generation: Tự động tạo URL cho danh mục và danh mục cha
- Badges/Flags: Nổi bật, xu hướng, hiển thị trang chủ
- Phân trang: Hỗ trợ pagination với thông tin chi tiết
Các bảng database liên quan:
- category_sanpham: Bảng chính chứa thông tin danh mục
- sanpham: Đếm số sản phẩm trong danh mục (trường 'cat')
Logic phân cấp danh mục:
- Danh mục cha: cat_main = 0, level = 1
- Danh mục con: cat_main = ID_cha, level = 2
- Sắp xếp: Theo cat_thutu (thứ tự), sau đó theo cat_id
- Icon xử lý: Tự động phân biệt icon ảnh và icon text
- URL slug: Sử dụng trường cat_blank làm slug
Các loại icon hỗ trợ:
- Icon ảnh: .jpg, .png, .gif, .jpeg, .webp
- Icon text: Emoji, text, hoặc mã icon font
- Icon mặc định: fa-folder nếu không có icon
Tham số lọc danh mục:
- type=all: Lấy tất cả danh mục (cha và con)
- type=parents: Chỉ lấy danh mục cha (cat_main = 0)
- type=children: Chỉ lấy danh mục con (cat_main > 0)
- parent_id: Lấy danh mục con của cha cụ thể
- include_children: Bao gồm danh mục con trong response
- include_products_count: Đếm số sản phẩm (có thể chậm)
Lưu ý:
- Bạn cần lấy token xác thực trước khi gọi API này
- include_products_count=1 có thể làm chậm API nếu có nhiều danh mục
- API tự động tạo URL đầy đủ cho tất cả danh mục
- Hỗ trợ phân trang với limit tối đa 200 danh mục/page
- Icon được xử lý thông minh (ảnh hoặc text)
- Danh mục con chỉ hiển thị khi include_children=1
- Thông tin danh mục cha được bao gồm cho danh mục con
- Badges hiển thị trạng thái đặc biệt của danh mục