Inventory stock update issue

Inventory stock update issue

MichalSmyk
Shopify Partner
2 0 0

Hi, 
I am trying to update stock via custom app. I was able to do it few days ago, but since yesterday I am only getting 400's errors. I have just managed to get 400 error. this is the endpoint : 

 

 

app.post('/update-stock', async (req, res) => {
    try {
        if (!Array.isArray(globalProductData.products)) {
            throw new Error("Global product data is not available.");
        }
        if (globalProductData && globalProductData.products) {
            for (const product of globalProductData.products) {
                console.log(`Product ID: ${product.id}, Title: ${product.title}`);
        
                if (product.variants && product.variants.length > 0) {
                    for (const variant of product.variants) {
                        console.log(`Variant ID: ${variant.id}, Title: ${variant.title}, SKU: ${variant.sku}, Inventory Item ID: ${variant.inventory_item_id}`);
                        
                        // Find the SKU quantity data for this variant
                        const skuQuantityData = skuQuantities.find(skuData => skuData.itemName === variant.sku);
                        console.log("SKU QTY", variant.sku)
                        if (skuQuantityData) {
                            // Use the SKU quantity data to update the stock
                            const stockData = {
                                "location_id": fulfillment_id,
                                "inventory_item_id": variant.inventory_item_id,
                                "available": skuQuantityData.unitsInStock
                            };
    
                            console.log("variant", variant.inventory_item_id);
                            console.log("QTY", skuQuantityData.unitsInStock);
    
                            const response = await fetch(updateStock, {
                                method: 'POST',
                                headers: {
                                    'X-Shopify-Access-Token': accessToken,
                                    'Content-Type': 'application/json',
                                },
                                body: stockData,
                            });
    
                            if (!response.ok) {
                                console.error(`HTTP error (update stock)! Status: ${response.status}`);
                            } else {
                                const data = await response.json();
                                console.log(`Stock updated for inventory_item_id ${variant.inventory_item_id}. Response: ${JSON.stringify(data)}`);
                            }
                        } else {
                            console.log(`SKU data not found for variant SKU: ${variant.sku}`);
                        }
    
                        // Introduce a delay of two seconds before processing the next variant
                        await new Promise(resolve => setTimeout(resolve, 2000));
                    }
                } else {
                    console.log('No variants found for this product.');
                }
            }
        }
        
        res.status(200).send("Stock updated successfully");
    } catch (error) {
        console.error(error);
        res.status(500).send("Error occurred while updating stock");
    }
});

 

 

 

this is Shopify's Api endpoint : 

`https://${shopName}/admin/api/2024-01/inventory_levels/[set.json](http://set.json)`
 
and console logs : 
Variant ID: 32907232565189, Title: Example, SKU: 12354G, Inventory Item ID: 34870360222237
SKU QTY 871010/G
variant 34870360113237
QTY 0
HTTP error (update stock)! Status: 400
Replies 4 (4)

SBD_
Shopify Staff
1829 271 413

Hey @MichalSmyk 

 

Are you able to capture a request ID from the headers of a 400 response? This will let us check the logs from our end.

Scott | Developer Advocate @ Shopify 

MichalSmyk
Shopify Partner
2 0 0

Hi @SBD_ 

 

I only get status 400 and Bad Request( not JSON )  in the response. I am really running out of ideas here. I use GET request to get products, and this confirms that the access token works, I gave all the permissions associated with orders/products/fulfilment to my app so this should work as well. 

We have one inventory that is run by 3rd party app, could it be the cause of the error to update the stock? 

 

Thanks

SBD_
Shopify Staff
1829 271 413

A 400 suggests the request isn't structured correctly. The request ID will be in the headers - are you able to log them?

Scott | Developer Advocate @ Shopify 

kh_shahzad_163
Visitor
1 0 0

I'm also facing error while updating Product Qty through CSV import option, and it does not updating the Qty column in the product, whereas the import showing no error while uploading and at the I also successful import email notification, and do update other columns, such as Price, but Qty column remain unchanged.

 

This process was working perfect since years but now it is skipping that Qty column since few days.