79689088

Date: 2025-07-03 15:21:07
Score: 0.5
Natty:
Report link
# Explanation and Fix

I hit this error while calling `cartLinesAdd`:

    {
        "data": {
            "cartLinesAdd": {
                "cart": null,
                "userErrors": [
                    {
                        "code": "INVALID",
                        "field": ["lines", "0", "quantity"],
                        "message": "The product 'X' is already sold out."
                    }
                ]
            }
        }
    }

It looks like an inventory problem, but the real culprit turned out to be **shipping rates**.

---

## What was really happening

- The product’s only stock was stored in a new location (`"Japan"`).  
- That location wasn’t covered by any shipping rate for the buyer’s country (`USA`).  
- During `cartLinesAdd`, Shopify checks:  
  *“Do I have at least one rate from the location that holds the stock to the destination country?”*  
- If the answer is **no**, the API returns the **“sold out”** error even when inventory exists.

---

## Extra troubleshooting step that revealed the root cause

In response to help from **Sabbir Noyon**, I logged the cart ID and full cart object inside the Remix action:

    export async function action({ request, context }: ActionFunctionArgs) {
        const { cart } = context;
        console.log('Cart context on server:', cart?.id);

        const formData = await request.formData();
        const { action, inputs } = CartForm.getFormInput(formData);

        if (action === CartForm.ACTIONS.LinesAdd) {
            const result = await cart.addLines(inputs.lines);
            console.log('Cart result from server:', result.cart);
            return result;
        }
    }

- The console showed a **valid cart ID**, so I copied that ID into **Postman** and manually called the Storefront API.  
- Postman reproduced the exact same JSON error, proving the issue was **not** a bad session or optimistic-UI glitch but a missing shipping rate for the new location.

---

## Fix

1. Go to **`Admin → Settings → Shipping and delivery`**.  
2. Open the **General profile**  
3. Add a simple **Free International Shipping** rate for the missing location and the `USA` zone.  
4. Save.  
5. `quantityAvailable` immediately jumped from `0` to the correct number and `cartLinesAdd` worked.

---

## Why it used to work

- Before creating the `"Japan"` location, stock lived in a location that already had rates for the USA.  
- After the inventory moved, there was no matching rate, so the API began to fail.

---

## Checklist for the future

- After adding a new location or moving stock, open the shipping profile and confirm **every location** has at least one rate for **every zone** you ship to.  
- If you use **Markets**, be sure the location is enabled under **Inventory & shipping origins** for that market.  
- The banner **“No rates for 1 location”** is a red flag—add or copy a rate before going live.  
- With a valid rate in place the product adds to cart without errors.

---

## Acknowledgements

Many thanks to **Sabbir Noyon** for the guidance.
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Jeremy Mejia