สรุปโดยย่อ
API ของ Magento 2 (Adobe Commerce) ช่วยให้นักพัฒนาสามารถผสานรวมกับร้านค้าอีคอมเมิร์ซได้โดยทางโปรแกรม โดยใช้ REST, SOAP และ GraphQL endpoint พร้อมกับการยืนยันตัวตนแบบ OAuth 1.0a และ token-based authentication ซึ่งให้การเข้าถึงผลิตภัณฑ์, คำสั่งซื้อ, ลูกค้า, สินค้าคงคลัง และอื่นๆ อีกมากมาย พร้อมกับขีดจำกัดอัตราที่กำหนดค่าได้ คู่มือนี้ครอบคลุมการตั้งค่าการยืนยันตัวตน, การดำเนินการ CRUD, webhooks, custom endpoints และกลยุทธ์การผสานรวมสำหรับการใช้งานจริง
บทนำ
Adobe Commerce (Magento) เป็นแพลตฟอร์มสำหรับร้านค้าอีคอมเมิร์ซมากกว่า 250,000 แห่ง ด้วยมูลค่าสินค้ารวมต่อปีมากกว่า 1.55 แสนล้านดอลลาร์ สำหรับนักพัฒนาที่สร้างการผสานรวมอีคอมเมิร์ซ, ตัวเชื่อมต่อ ERP หรือแอปพลิเคชันมือถือ การผสานรวม Magento API ไม่ใช่ทางเลือก แต่เป็นสิ่งจำเป็นสำหรับการเข้าถึงฐานผู้ค้าขนาดใหญ่นี้
ความเป็นจริงคือ: ผู้ค้าที่จัดการช่องทางการขายหลายช่องทางเสียเวลา 20-30 ชั่วโมงต่อสัปดาห์ในการป้อนข้อมูลด้วยตนเองระหว่าง Magento และระบบอื่นๆ การผสานรวม Magento API ที่แข็งแกร่งช่วยให้การซิงโครไนซ์ผลิตภัณฑ์, การประมวลผลคำสั่งซื้อ, การอัปเดตสินค้าคงคลัง และการจัดการข้อมูลลูกค้าเป็นไปโดยอัตโนมัติ
คู่มือนี้จะแนะนำคุณตลอดกระบวนการผสานรวม Magento 2 API ที่สมบูรณ์ คุณจะได้เรียนรู้การยืนยันตัวตนแบบ OAuth 1.0a และ token, REST/SOAP/GraphQL endpoint, การจัดการผลิตภัณฑ์และคำสั่งซื้อ, webhooks, การพัฒนา API แบบกำหนดเอง และกลยุทธ์การปรับใช้สำหรับการใช้งานจริง เมื่ออ่านจบ คุณจะมีการผสานรวม Magento ที่พร้อมใช้งานจริง
Magento 2 API คืออะไร?
Magento 2 มี API สามประเภทสำหรับการเข้าถึงข้อมูลอีคอมเมิร์ซ:
- REST API: ใช้ JSON สำหรับเว็บและแอปพลิเคชันมือถือ
- SOAP API: ใช้ XML สำหรับการผสานรวมระดับองค์กร
- GraphQL: ใช้การสอบถามข้อมูลเพื่อแอปพลิเคชันส่วนหน้าที่มีประสิทธิภาพ
API จัดการสิ่งเหล่านี้:
- ผลิตภัณฑ์, หมวดหมู่ และสินค้าคงคลัง
- คำสั่งซื้อ, ใบแจ้งหนี้ และการจัดส่ง
- ลูกค้าและกลุ่มลูกค้า
- ตะกร้าสินค้าและการชำระเงิน
- โปรโมชันและกฎการกำหนดราคา
- หน้าและบล็อก CMS
- การกำหนดค่าร้านค้า
คุณสมบัติหลัก
| คุณสมบัติ | คำอธิบาย |
|---|---|
| หลายโปรโตคอล | REST, SOAP, GraphQL |
| OAuth 1.0a | การเข้าถึงจากบุคคลที่สามที่ปลอดภัย |
| การยืนยันตัวตนด้วยโทเค็น | โทเค็นผู้ดูแลระบบและโทเค็นการผสานรวม |
| Webhooks | การดำเนินการแบบอะซิงโครนัสผ่านคิว |
| การจำกัดอัตรา | กำหนดค่าได้ต่อการติดตั้ง |
| Custom Endpoints | ขยายด้วย API ที่กำหนดเอง |
| หลายร้านค้า | API เดียว, มุมมองร้านค้าหลายมุมมอง |
การเปรียบเทียบ API
| ประเภท API | โปรโตคอล | กรณีการใช้งาน |
|---|---|---|
| REST | JSON | แอปพลิเคชันมือถือ, การผสานรวม |
| SOAP | XML | ระบบองค์กร (SAP, Oracle) |
| GraphQL | GraphQL | หน้าร้าน, PWA |
เวอร์ชันของ Magento
| เวอร์ชัน | สถานะ | สิ้นสุดการสนับสนุน |
|---|---|---|
| Magento 2.4.x | ปัจจุบัน | ใช้งานอยู่ |
| Adobe Commerce 2.4.x | ปัจจุบัน | ใช้งานอยู่ |
| Magento 1.x | EOL (สิ้นสุดการใช้งาน) | มิถุนายน 2020 (ไม่ควรใช้) |
เริ่มต้นใช้งาน: การตั้งค่าการยืนยันตัวตน
ขั้นตอนที่ 1: สร้างบัญชีผู้ดูแลระบบหรือการผสานรวม
ก่อนเข้าถึง API:
- เข้าสู่ระบบ Magento Admin Panel
- ไปที่ System > Permissions > All Users
- สร้างผู้ใช้ผู้ดูแลระบบ (สำหรับโทเค็นผู้ดูแลระบบ) หรือ
- ไปที่ System > Extensions > Integrations
- สร้างการผสานรวมใหม่ (สำหรับ OAuth)
ขั้นตอนที่ 2: เลือกวิธีการยืนยันตัวตน
| วิธีการ | เหมาะสำหรับ | อายุการใช้งานโทเค็น |
|---|---|---|
| โทเค็นผู้ดูแลระบบ | การผสานรวมภายใน | กำหนดค่าได้ (ค่าเริ่มต้น: 4 ชั่วโมง) |
| โทเค็นการผสานรวม | แอปพลิเคชันจากบุคคลที่สาม | จนกว่าจะถูกเพิกถอน |
| OAuth 1.0a | แอปพลิเคชันในตลาดสาธารณะ | จนกว่าจะถูกเพิกถอน |
| โทเค็นลูกค้า | แอปพลิเคชันสำหรับลูกค้า | กำหนดค่าได้ |
ขั้นตอนที่ 3: รับโทเค็นผู้ดูแลระบบ (วิธีที่ง่ายที่สุด)
สร้างโทเค็นผู้ดูแลระบบสำหรับการผสานรวมภายใน:
const MAGENTO_BASE_URL = process.env.MAGENTO_BASE_URL;
const MAGENTO_ADMIN_USERNAME = process.env.MAGENTO_ADMIN_USERNAME;
const MAGENTO_ADMIN_PASSWORD = process.env.MAGENTO_ADMIN_PASSWORD;
const getAdminToken = async () => {
const response = await fetch(`${MAGENTO_BASE_URL}/rest/V1/integration/admin/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: MAGENTO_ADMIN_USERNAME,
password: MAGENTO_ADMIN_PASSWORD
})
});
if (!response.ok) {
throw new Error('Invalid admin credentials');
}
// Response is a plain string (the token), not JSON
const token = await response.text();
return token;
};
// Usage
const token = await getAdminToken();
console.log(`Admin token: ${token}`);
// Store securely - use for subsequent API calls
หมายเหตุความปลอดภัย: จัดเก็บโทเค็นอย่างปลอดภัย:
# .env file
MAGENTO_BASE_URL="https://store.example.com"
MAGENTO_ADMIN_USERNAME="api_user"
MAGENTO_ADMIN_PASSWORD="secure_password_here"
MAGENTO_ACCESS_TOKEN="obtained_via_auth"
ขั้นตอนที่ 4: สร้างการผสานรวม (แนะนำสำหรับบุคคลที่สาม)
สร้างการผสานรวมผ่าน Admin Panel:
ไปที่ System > Extensions > Integrations
คลิก Add New Integration
กรอกรายละเอียด:
- ชื่อ: “My Integration”
- อีเมล: your-email@example.com
- Callback URL: (สำหรับ OAuth)
- Identity Link URL: (สำหรับ OAuth)
ตั้งค่า API Permissions:
- ทรัพยากร: เลือกสิทธิ์ที่ต้องการ
- แนะนำ: ผลิตภัณฑ์, คำสั่งซื้อ, ลูกค้า, สินค้าคงคลัง
คลิก Save
คลิก Activate ในการผสานรวมใหม่
คัดลอก Access Token และ Token Secret
ขั้นตอนที่ 5: รับโทเค็นลูกค้า
สำหรับแอปพลิเคชันที่ลูกค้าใช้งาน:
const getCustomerToken = async (email, password) => {
const response = await fetch(`${MAGENTO_BASE_URL}/rest/V1/integration/customer/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: email,
password: password
})
});
if (!response.ok) {
throw new Error('Invalid customer credentials');
}
const token = await response.text();
return token;
};
// Usage
const customerToken = await getCustomerToken('customer@example.com', 'password123');
ขั้นตอนที่ 6: เรียกใช้ API ที่มีการยืนยันตัวตน
สร้างไคลเอ็นต์ API ที่สามารถนำมาใช้ซ้ำได้:
const magentoRequest = async (endpoint, options = {}) => {
const token = await getAdminToken(); // Or retrieve stored token
const response = await fetch(`${MAGENTO_BASE_URL}/rest${endpoint}`, {
...options,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
...options.headers
}
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Magento API Error: ${error.message}`);
}
return response.json();
};
// Usage
const products = await magentoRequest('/V1/products');
console.log(`Found ${products.items.length} products`);
การจัดการผลิตภัณฑ์
การดึงข้อมูลผลิตภัณฑ์
ดึงข้อมูลผลิตภัณฑ์พร้อมการกรอง:
const getProducts = async (filters = {}) => {
const params = new URLSearchParams();
// Build search criteria
if (filters.search) {
params.append('searchCriteria[filterGroups][0][filters][0][field]', 'sku');
params.append('searchCriteria[filterGroups][0][filters][0][value]', `%${filters.search}%`);
params.append('searchCriteria[filterGroups][0][filters][0][conditionType]', 'like');
}
if (filters.priceFrom) {
params.append('searchCriteria[filterGroups][1][filters][0][field]', 'price');
params.append('searchCriteria[filterGroups][1][filters][0][value]', filters.priceFrom);
params.append('searchCriteria[filterGroups][1][filters][0][conditionType]', 'gteq');
}
params.append('searchCriteria[pageSize]', filters.limit || 20);
params.append('searchCriteria[currentPage]', filters.page || 1);
const response = await magentoRequest(`/V1/products?${params.toString()}`);
return response;
};
// Usage
const products = await getProducts({ search: 'shirt', priceFrom: 20, limit: 50 });
products.items.forEach(product => {
console.log(`${product.sku}: ${product.name} - $${product.price}`);
});
การดึงข้อมูลผลิตภัณฑ์เดียว
ดึงข้อมูลผลิตภัณฑ์ด้วย SKU:
const getProduct = async (sku) => {
const response = await magentoRequest(`/V1/products/${sku}`);
return response;
};
// Usage
const product = await getProduct('TSHIRT-001');
console.log(`Name: ${product.name}`);
console.log(`Price: $${product.price}`);
console.log(`Stock: ${product.extension_attributes?.stock_item?.qty}`);
การสร้างผลิตภัณฑ์
สร้างผลิตภัณฑ์แบบ Simple:
const createProduct = async (productData) => {
const product = {
product: {
sku: productData.sku,
name: productData.name,
attribute_set_id: productData.attributeSetId || 4, // Default set
type_id: 'simple',
price: productData.price,
status: productData.status || 1, // 1=enabled, 2=disabled
visibility: productData.visibility || 4, // 4=Catalog & Search
weight: productData.weight || 1,
extension_attributes: {
stock_item: {
qty: productData.qty || 0,
is_in_stock: productData.qty > 0 ? true : false
}
},
custom_attributes: [
{
attribute_code: 'description',
value: productData.description
},
{
attribute_code: 'short_description',
value: productData.shortDescription
},
{
attribute_code: 'color',
value: productData.color
},
{
attribute_code: 'size',
value: productData.size
}
]
}
};
const response = await magentoRequest('/V1/products', {
method: 'POST',
body: JSON.stringify(product)
});
return response;
};
// Usage
const newProduct = await createProduct({
sku: 'TSHIRT-NEW-001',
name: 'Premium Cotton T-Shirt',
price: 29.99,
qty: 100,
description: 'High-quality cotton t-shirt',
shortDescription: 'Premium cotton tee',
color: 'Blue',
size: 'M'
});
console.log(`Product created: ${newProduct.id}`);
การอัปเดตผลิตภัณฑ์
อัปเดตข้อมูลผลิตภัณฑ์:
const updateProduct = async (sku, updates) => {
const product = {
product: {
sku: sku,
...updates
}
};
const response = await magentoRequest(`/V1/products/${sku}`, {
method: 'PUT',
body: JSON.stringify(product)
});
return response;
};
// Usage - Update price and stock
await updateProduct('TSHIRT-001', {
price: 24.99,
extension_attributes: {
stock_item: {
qty: 150,
is_in_stock: true
}
}
});
การลบผลิตภัณฑ์
ลบผลิตภัณฑ์:
const deleteProduct = async (sku) => {
await magentoRequest(`/V1/products/${sku}`, {
method: 'DELETE'
});
console.log(`Product ${sku} deleted`);
};
ประเภทผลิตภัณฑ์
| ประเภท | คำอธิบาย | กรณีการใช้งาน |
|---|---|---|
| Simple | SKU เดียว, ไม่มีตัวเลือก | ผลิตภัณฑ์มาตรฐาน |
| Configurable | ผลิตภัณฑ์หลักที่มีตัวเลือกย่อย | ตัวเลือกขนาด/สี |
| Grouped | การรวมผลิตภัณฑ์แบบ Simple หลายรายการ | ชุดผลิตภัณฑ์ |
| Virtual | ผลิตภัณฑ์ที่ไม่ใช่กายภาพ | บริการ, การดาวน์โหลด |
| Bundle | ชุดผลิตภัณฑ์ที่ปรับแต่งได้ | ชุดอุปกรณ์สร้างเอง |
| Downloadable | ผลิตภัณฑ์ดิจิทัล | E-books, ซอฟต์แวร์ |
การจัดการคำสั่งซื้อ
การดึงข้อมูลคำสั่งซื้อ
ดึงข้อมูลคำสั่งซื้อพร้อมการกรอง:
const getOrders = async (filters = {}) => {
const params = new URLSearchParams();
if (filters.status) {
params.append('searchCriteria[filterGroups][0][filters][0][field]', 'status');
params.append('searchCriteria[filterGroups][0][filters][0][value]', filters.status);
params.append('searchCriteria[filterGroups][0][filters][0][conditionType]', 'eq');
}
if (filters.dateFrom) {
params.append('searchCriteria[filterGroups][1][filters][0][field]', 'created_at');
params.append('searchCriteria[filterGroups][1][filters][0][value]', filters.dateFrom);
params.append('searchCriteria[filterGroups][1][filters][0][conditionType]', 'gteq');
}
params.append('searchCriteria[pageSize]', filters.limit || 20);
params.append('searchCriteria[currentPage]', filters.page || 1);
const response = await magentoRequest(`/V1/orders?${params.toString()}`);
return response;
};
// Usage - Get pending orders from last 7 days
const orders = await getOrders({
status: 'pending',
dateFrom: '2026-03-18 00:00:00',
limit: 50
});
orders.items.forEach(order => {
console.log(`Order #${order.increment_id}: ${order.customer_email} - $${order.grand_total}`);
});
การดึงข้อมูลคำสั่งซื้อเดียว
ดึงข้อมูลคำสั่งซื้อด้วย ID:
const getOrder = async (orderId) => {
const response = await magentoRequest(`/V1/orders/${orderId}`);
return response;
};
// Usage
const order = await getOrder(12345);
console.log(`Order #${order.increment_id}`);
console.log(`Status: ${order.status}`);
console.log(`Total: $${order.grand_total}`);
console.log(`Items:`);
order.items.forEach(item => {
console.log(` - ${item.name} x ${item.qty_ordered}`);
});
ลำดับสถานะคำสั่งซื้อ
pending → processing → complete
→ canceled
→ on_hold
→ payment_review
การอัปเดตสถานะคำสั่งซื้อ
เปลี่ยนสถานะคำสั่งซื้อ:
const updateOrderStatus = async (orderId, newStatus) => {
// Note: Direct status update requires custom endpoint
// Use order management workflow instead:
// For cancel:
await magentoRequest(`/V1/orders/${orderId}/cancel`, {
method: 'POST'
});
// For hold:
await magentoRequest(`/V1/orders/${orderId}/hold`, {
method: 'POST'
});
// For unhold:
await magentoRequest(`/V1/orders/${orderId}/unhold`, {
method: 'POST'
});
};
การสร้างใบแจ้งหนี้
สร้างใบแจ้งหนี้สำหรับคำสั่งซื้อ:
const createInvoice = async (orderId, items = [], notify = true, appendComment = false, comment = null) => {
const invoice = {
capture: true, // true = capture payment
last: true,
items: items // Array of {order_item_id, qty}
};
if (comment) {
invoice.comment = comment;
invoice.notify_customer = notify ? 1 : 0;
invoice.append_comment = appendComment ? 1 : 0;
}
const response = await magentoRequest(`/V1/order/${orderId}/invoice`, {
method: 'POST',
body: JSON.stringify(invoice)
});
return response;
};
// Usage - Invoice and capture full order
const invoiceId = await createInvoice(12345, [], true, false, 'Thank you for your order!');
console.log(`Invoice created: ${invoiceId}`);
การสร้างการจัดส่ง
จัดส่งคำสั่งซื้อ:
const createShipment = async (orderId, items = [], notify = true, appendComment = false, comment = null, tracks = []) => {
const shipment = {
items: items, // Array of {order_item_id, qty}
notify: notify ? 1 : 0,
append_comment: appendComment ? 1 : 0,
comment: comment,
tracks: tracks // Array of {track_number, title, carrier_code}
};
const response = await magentoRequest(`/V1/order/${orderId}/ship`, {
method: 'POST',
body: JSON.stringify(shipment)
});
return response;
};
// Usage - Ship with tracking
const shipmentId = await createShipment(12345, [], true, false, 'Your order has shipped!', [
{
track_number: '1Z999AA10123456784',
title: 'Tracking Number',
carrier_code: 'ups'
}
]);
console.log(`Shipment created: ${shipmentId}`);
การจัดการลูกค้า
การดึงข้อมูลลูกค้า
ดึงข้อมูลลูกค้า:
const getCustomers = async (filters = {}) => {
const params = new URLSearchParams();
if (filters.email) {
params.append('searchCriteria[filterGroups][0][filters][0][field]', 'email');
params.append('searchCriteria[filterGroups][0][filters][0][value]', filters.email);
params.append('searchCriteria[filterGroups][0][filters][0][conditionType]', 'eq');
}
params.append('searchCriteria[pageSize]', filters.limit || 20);
const response = await magentoRequest(`/V1/customers/search?${params.toString()}`);
return response;
};
// Usage
const customers = await getCustomers({ email: 'customer@example.com' });
customers.items.forEach(customer => {
console.log(`${customer.firstname} ${customer.lastname} - ${customer.email}`);
});
การสร้างลูกค้า
ลงทะเบียนลูกค้าใหม่:
const createCustomer = async (customerData) => {
const customer = {
customer: {
websiteId: customerData.websiteId || 1,
email: customerData.email,
firstname: customerData.firstname,
lastname: customerData.lastname,
middlename: customerData.middlename || '',
gender: customerData.gender || 0,
store_id: customerData.storeId || 0,
extension_attributes: {
is_subscribed: customerData.subscribed || false
}
},
password: customerData.password
};
const response = await magentoRequest('/V1/customers', {
method: 'POST',
body: JSON.stringify(customer)
});
return response;
};
// Usage
const newCustomer = await createCustomer({
email: 'newcustomer@example.com',
firstname: 'John',
lastname: 'Doe',
password: 'SecurePass123!',
subscribed: true
});
console.log(`Customer created: ID ${newCustomer.id}`);
การจัดการสินค้าคงคลัง (MSI)
การดึงข้อมูลสถานะสต็อก
ตรวจสอบสต็อกสินค้า:
const getStockStatus = async (sku) => {
const response = await magentoRequest(`/V1/products/${sku}/stockItems/1`);
return response;
};
// Usage
const stock = await getStockStatus('TSHIRT-001');
console.log(`Qty: ${stock.qty}`);
console.log(`In Stock: ${stock.is_in_stock}`);
console.log(`Min Qty: ${stock.min_qty}`);
การอัปเดตสต็อก
อัปเดตปริมาณสินค้า:
const updateStock = async (sku, qty, isInStock = null) => {
const stockItem = {
stockItem: {
qty: qty,
is_in_stock: isInStock !== null ? isInStock : qty > 0
}
};
const response = await magentoRequest(`/V1/products/${sku}/stockItems/1`, {
method: 'PUT',
body: JSON.stringify(stockItem)
});
return response;
};
// Usage
await updateStock('TSHIRT-001', 100, true);
Webhooks และการดำเนินการแบบอะซิงโครนัส
การตั้งค่า Webhooks
Magento ใช้คิวข้อความสำหรับการแจ้งเตือนแบบอะซิงโครนัส:
// Magento doesn't have native webhooks
// Use these approaches instead:
// 1. Poll orders endpoint periodically
const pollNewOrders = async (lastOrderId) => {
const orders = await getOrders({
dateFrom: new Date().toISOString()
});
const newOrders = orders.items.filter(o => o.id > lastOrderId);
return newOrders;
};
// 2. Use Adobe I/O Events (Adobe Commerce only)
// Configure events in Adobe Developer Console
// 3. Create custom webhook module
// See: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/message-queues/message-queues.html
การจำกัดอัตรา (Rate Limiting)
การทำความเข้าใจการจำกัดอัตรา
การจำกัดอัตราของ Magento สามารถกำหนดค่าได้:
- ค่าเริ่มต้น: ไม่มีขีดจำกัด (กำหนดค่าได้ใน Admin)
- แนะนำ: 100-1000 คำขอ/นาที
กำหนดค่าใน Admin: Stores > Configuration > Services > Web API > Security
การนำการจัดการการจำกัดอัตราไปใช้
const makeRateLimitedRequest = async (endpoint, options = {}, maxRetries = 3) => {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const response = await magentoRequest(endpoint, options);
return response;
} catch (error) {
if (error.message.includes('429') && attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
} else {
throw error;
}
}
}
};
รายการตรวจสอบสำหรับการปรับใช้จริง
ก่อนใช้งานจริง:
- [ ] ใช้โทเค็นการผสานรวม (ไม่ใช่ข้อมูลประจำตัวผู้ดูแลระบบ) ในสภาพแวดล้อมจริง
- [ ] จัดเก็บโทเค็นอย่างปลอดภัย (ฐานข้อมูลที่เข้ารหัส)
- [ ] นำการจำกัดอัตราและการจัดคิวคำขอไปใช้
- [ ] เพิ่มการจัดการข้อผิดพลาดที่ครอบคลุม
- [ ] ตั้งค่าการบันทึกสำหรับการเรียกใช้ API ทั้งหมด
- [ ] สร้างทางเลือก webhook (การโพลลิ่ง หรือ Adobe I/O)
- [ ] ทดสอบด้วยปริมาณข้อมูลจริง
- [ ] นำตรรกะการลองใหม่สำหรับคำขอที่ล้มเหลวไปใช้
กรณีการใช้งานจริง
การผสานรวม ERP
ผู้ผลิตทำการซิงค์สินค้าคงคลัง:
- ความท้าทาย: การอัปเดตสต็อกด้วยตนเองระหว่าง ERP และ Magento
- วิธีแก้ปัญหา: การซิงค์ API สองทางทุก 15 นาที
- ผลลัพธ์: สินค้าคงคลังแบบเรียลไทม์, ไม่มีการขายเกินสต็อก
แอปพลิเคชันมือถือ
ผู้ค้าปลีกสร้างแอปพลิเคชันช้อปปิ้ง:
- ความท้าทาย: ต้องการประสบการณ์มือถือแบบเนทีฟ
- วิธีแก้ปัญหา: GraphQL API สำหรับการเรียกดูผลิตภัณฑ์, REST สำหรับการชำระเงิน
- ผลลัพธ์: อัตราการแปลงบนมือถือเพิ่มขึ้น 40%
สรุป
Magento 2 API มอบคุณสมบัติอีคอมเมิร์ซที่ครอบคลุม ประเด็นสำคัญ:
- มี API ทั้ง REST, SOAP และ GraphQL
- การยืนยันตัวตนด้วยโทเค็นสำหรับการผสานรวม
- การดำเนินการ CRUD เต็มรูปแบบสำหรับผลิตภัณฑ์, คำสั่งซื้อ, ลูกค้า
- MSI สำหรับการจัดการสินค้าคงคลังขั้นสูง
- การจำกัดอัตราที่สามารถกำหนดค่าได้ต่อการติดตั้ง
- Apidog ช่วยให้การทดสอบ API และการทำงานร่วมกันในทีมเป็นไปอย่างราบรื่น
ส่วนคำถามที่พบบ่อย (FAQ)
ฉันจะยืนยันตัวตนกับ Magento API ได้อย่างไร?
ใช้โทเค็นผู้ดูแลระบบสำหรับการผสานรวมภายใน หรือสร้างการผสานรวมใน System > Extensions สำหรับ OAuth โทเค็นลูกค้าสำหรับแอปพลิเคชันที่ลูกค้าใช้งาน
ความแตกต่างระหว่าง REST และ GraphQL ใน Magento คืออะไร?
REST ให้การดำเนินการ CRUD เต็มรูปแบบ ส่วน GraphQL ได้รับการปรับให้เหมาะสมสำหรับการสอบถามข้อมูลจากส่วนหน้าด้วยการดึงข้อมูลที่มีประสิทธิภาพ
ฉันจะสร้างผลิตภัณฑ์ผ่าน API ได้อย่างไร?
ส่งคำขอ POST ไปยัง /V1/products พร้อมข้อมูลผลิตภัณฑ์ รวมถึง SKU, ชื่อ, ราคา และ stock_item ใน extension_attributes
ฉันสามารถรับ webhooks สำหรับคำสั่งซื้อใหม่ได้หรือไม่?
Magento ไม่มี webhooks ในตัว ให้ใช้การโพลลิ่ง, Adobe I/O Events (สำหรับ Adobe Commerce) หรือสร้างโมดูลที่กำหนดเอง
ฉันจะอัปเดตปริมาณสต็อกได้อย่างไร?
ส่งคำขอ PUT ไปยัง /V1/products/{sku}/stockItems/1 ด้วยค่า qty และ is_in_stock
