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

Magento 2 REST API:下单前检查购物车商品库存状态方法咨询

Alright, let's work through your Magento 2 mobile app issue and the REST API question—here's a clear breakdown of what to do:

解决当前订单缺货问题

First, let's fix the immediate problem with your order and prevent this from happening again:

  • Resolve the existing order & restore inventory
    Since this was a test purchase by your friend, you can easily roll back the stock and cancel the test order:

    1. Log into your Magento admin panel, go to Sales > Orders, and locate the test order that drained the stock.
    2. Open the order and click Cancel. By default, Magento will automatically restore the product's inventory when an order is canceled (you can confirm this setting at Stores > Configuration > Catalog > Inventory > Stock Options under Backorder Options).
    3. If stock doesn't restore automatically, head to Catalog > Products, find the affected item, edit its Quantity field under the Inventory tab, and save the changes.
  • Prevent future out-of-stock surprises
    To stop users from adding items to their cart that can't be fulfilled, tweak these inventory settings:

    1. In admin, go to Stores > Configuration > Catalog > Inventory > Stock Options.
    2. Enable Decrease Stock When Order is Placed (this is usually on by default, but double-check) to lock stock as soon as an order is submitted.
    3. Set Set Items to be Out of Stock When Quantity is Below to 0—this will hide or disable out-of-stock items from being added to carts entirely.
    4. Ensure Use Magento Stock Management is enabled to keep inventory synced across all channels (including your mobile app and test scenarios).
通过REST API检查购物车中所有商品的库存状态

To proactively check stock status for items in a user's cart via Magento 2 REST API, follow these steps (note: you'll need authentication tokens for these requests):

  1. Get a customer authentication token (for checking the current user's cart)
    Send a POST request to /rest/V1/integration/customer/token with this body:

    {
      "username": "customer@example.com",
      "password": "customer_password"
    }
    

    Include the returned token in your request headers as Authorization: Bearer <your_token>.

  2. Fetch all items in the user's cart
    Send a GET request to /rest/V1/carts/mine/items (for the authenticated customer's cart). The response will include each item's sku, qty, and other details.

  3. Batch-check stock status for cart items
    Use a POST request to /rest/V1/products/stockItems with a list of SKUs from the cart:

    {
      "skus": ["your-product-sku-1", "your-product-sku-2"]
    }
    

    The response will return key stock details for each item, including:

    • is_in_stock: Boolean indicating if the item is available
    • qty: Current stock quantity
    • stock_id: The inventory stock ID (default is 1 for the main stock)

    Alternatively, to check a single item's stock, send a GET request to /rest/V1/products/:sku/stockItem (replace :sku with the item's SKU).

  4. Add logic to your mobile app
    In your app, combine these steps: fetch the cart items, extract their SKUs, run the batch stock check, then compare each item's available stock with the quantity in the cart. If any item's stock is less than the cart quantity, show a warning to the user before they attempt to submit an order.

Quick notes

  • Make sure your API user (customer or admin) has the correct permissions: admins need Magento_Catalog::inventory access, while customers need basic Magento_Customer::customer permissions.
  • If you're using multi-source inventory, specify the correct stock_id in your requests to check the right warehouse's stock.

内容的提问来源于stack exchange,提问作者Jsparo30

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:17:18