You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过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)

额外排查点

  1. 确认商品属性集(attribute_set_id:4)包含库存相关属性
  2. 检查后台全局库存配置:Stores > Configuration > Catalog > Inventory,确保无全局规则覆盖商品级库存设置

内容的提问来源于stack exchange,提问作者Anup Kumar Panwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 22:12:21