通过Magento REST API添加商品指定库存仍显示缺货
Magento 2 API创建商品时库存设置不生效的解决方案
你的问题核心是库存配置参数缺失,尽管指定了qty和is_in_stock,但Magento默认会继承系统库存管理规则,导致手动设置的库存状态未生效。
修正后的CURL请求
在stock_item中补充启用手动库存管理的参数,确保设置的库存数量和状态被系统识别:
curl --location '<store_url>/rest/V1/products' \ --header 'Authorization: Bearer <secret_key>' \ --header 'Content-Type: application/json' \ --data '{ "product": { "sku": "380000", "name": "Men Plus Size Groube Striped Shirt", "attribute_set_id": 4, "price": "899.00", "weight": 0, "type_id": "simple", "status":1, "visibility":4, "custom_attributes": [ { "attribute_code": "description", "value": "Shirt in super combed cotton." }, { "attribute_code": "image", "value": "https://cdn.shopify.com/s/files/1/0591/5223/6751/products/Groube.Main.jpg?v=1676974530" }, { "attribute_code": "url_key", "value": "men-plus-tshrt" }, { "attribute_code": "brand_name", "value": "anup" }, { "attribute_code": "tag", "value": "" } ], "extension_attributes": { "stock_item": { "qty": 4, "is_in_stock": true, "manage_stock": true, "use_config_manage_stock": false, "stock_id": 1 } } } }'
关键参数说明
manage_stock: true:开启当前商品的库存管理功能use_config_manage_stock: false:禁用系统默认库存配置继承,强制使用当前商品的库存设置stock_id: 1:指定默认库存ID(Magento默认库存ID通常为1,若你的店铺使用自定义库存需替换对应ID)
额外排查点
- 确认商品属性集(
attribute_set_id:4)包含库存相关属性 - 检查后台全局库存配置:Stores > Configuration > Catalog > Inventory,确保无全局规则覆盖商品级库存设置
内容的提问来源于stack exchange,提问作者Anup Kumar Panwar
相关产品推荐
相关产品推荐

