cancel
Showing results for 
Search instead for 
Did you mean: 

Updating Stock Status Via REST API

SOLVED

Updating Stock Status Via REST API

Hi guys
I'm having a problem updating a stock status when updating it via API.

I've been getting this error
"The stock item was unable to be saved. Please try again."

But it works just fine with the same request from Postman

I'm following the SWAGGER documentation

 

 

What works in postman:

https://store/index.php/rest/V1/products/DIG11925/stockItems/2725
headers:
content-type: application/json

Authorization: Bearer rgme26l6eg1rkoftm0f510qi0zzexk24g

body:

{
"stockItem": {
"is_in_stock": true,
"qty": 1
}
}

When performing this request in postman I'm getting back

"2725" (which is itemid) and item gets updated just fine
 
if I perform the same in JS script I'm always getting a "The stock item was unable to be saved. Please try again."
 
I've checked the script 100 times and I can't see anything different in it....

                    var stockDataNewJSON = {};
                        stockDataNewJSON.stockItem = {};
                        stockDataNewJSON.stockItem.qty = Number(document.getElementById("quantity-held").value);
                        if (document.getElementById("inputGroupSelect01").value === 1) {
                            stockDataNewJSON.stockItem.is_in_stock = true;
                        }
                        else {
                            stockDataNewJSON.stockItem.is_in_stock = false;
                        }
                    fetch('https://store/index.php/rest/V1/products/' + productData.sku + '/stockItems/' + stockData.item_id, {
                            method: 'PUT',
                            headers: {
                                'content-type': 'application/json',
                                'Authorization': 'Bearer ' + magentoToken
                            },
                            body: JSON.stringify(stockDataNewJSON)
                        }).then((response) => {
                            return response.json();
                        }).then((data) => {
                            resolve(data);
                            console.log(data);
                            alert(JSON.stringify(data));
                        });
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Updating Stock Status Via REST API

Hi @roman_polevecko 

Please follow below steps to update status of stock using rest api:
API url : http://<baseUrl>/rest/V1/products/my-sku/stockItems/16
Token : Admin Token
Method : PUT
Request:

{"stockItem":{"qty":2000,"is_in_stock":true}}

update_stock.png

 

Status in admin:
stock_admin.png

 

If product is not updated after this step, please run below command :

php bin/magento indexer:reindex

and also check Index Management, mode should be Update on Save .
In Admin > System > Tools > Index Management.
index_management.png

 

It may help to resolve your issue.

If issue resolve, please click on 'Kudos' & Accept as Solution!

 

 

Problem solved? Click Accept as Solution!

View solution in original post

1 REPLY 1

Re: Updating Stock Status Via REST API

Hi @roman_polevecko 

Please follow below steps to update status of stock using rest api:
API url : http://<baseUrl>/rest/V1/products/my-sku/stockItems/16
Token : Admin Token
Method : PUT
Request:

{"stockItem":{"qty":2000,"is_in_stock":true}}

update_stock.png

 

Status in admin:
stock_admin.png

 

If product is not updated after this step, please run below command :

php bin/magento indexer:reindex

and also check Index Management, mode should be Update on Save .
In Admin > System > Tools > Index Management.
index_management.png

 

It may help to resolve your issue.

If issue resolve, please click on 'Kudos' & Accept as Solution!

 

 

Problem solved? Click Accept as Solution!