Magento 2 REST API如何获取可配置产品关联的带颜色尺寸的简单产品详情
Great question! When working with Magento 2's REST API to fetch configurable products and their associated simple products (complete with color and size details), here's a straightforward, step-by-step approach:
You have two main ways to get the simple products linked to your configurable items:
Option A: Fetch for a Single Configurable Product
If you want to target one configurable product at a time, use the dedicated configurable product children endpoint. Replace {SKU} with your configurable product's SKU:
GET example.net/index.php/rest/V1/configurable-products/{SKU}/children
This endpoint returns basic details for each associated simple product, including its SKU, ID, and the value IDs for linked attributes like color and size.
Option B: Batch Fetch with Your Existing Configurable Product Query
To grab configurable products and their linked simple product IDs in one request, add a fields parameter to your existing search URL. This filters the response to include only the data you need:
GET example.net/index.php/rest/V1/products?searchCriteria[pageSize]=5&searchCriteria[currentPage]=1&searchCriteria[filterGroups][0][filters][0][field]=type_id&searchCriteria[filterGroups][0][filters][0][value]=configurable&searchCriteria[filterGroups][0][filters][0][conditionType]=eq&fields=items[id,sku,extension_attributes[configurable_product_links]]
The extension_attributes.configurable_product_links array in each result will contain the entity IDs of all associated simple products.
Once you have the simple product IDs/SKUs, you can fetch their complete details—including color and size attributes—using either single or batch requests.
Batch Request (Recommended for Efficiency)
Use a POST request to /V1/products/list to fetch multiple simple products at once. Here's an example request body (replace the entity_id values with your simple product IDs):
{ "searchCriteria": { "filterGroups": [ { "filters": [ { "field": "entity_id", "value": "101,102,103", "conditionType": "in" } ] } ], "fields": "items[id,sku,name,price,custom_attributes[attribute_code,value]]" } }
The custom_attributes section will include entries for color and size (assuming these attributes are set to be visible in the API). Each entry has an attribute_code (to identify the attribute) and value (the attribute's value ID).
The API returns attribute value IDs (e.g., 12 for "Red") instead of human-readable labels. To map these IDs to names, call the attribute options endpoint for color and size:
- For color:
GET example.net/index.php/rest/V1/products/attributes/color/options - For size:
GET example.net/index.php/rest/V1/products/attributes/size/options
Each response will include a list of options with label (the readable name) and value (the ID you saw in the simple product details). You can store this mapping to translate IDs into labels for your frontend or application.
- Ensure your API integration user has the
catalog_product_readpermission (set in Magento Admin under System > Permissions > Users). - Double-check that your color and size attributes have the Visible in API setting enabled (found in the attribute edit page under Storefront Properties).
- For large batches of products, split requests into smaller chunks to avoid timeouts or performance issues.
内容的提问来源于stack exchange,提问作者Shozib Rasool

