I'm attempting to use cURL to update the stock for an item - it connects, and the item exists (tried using an invalid sku to test), using the below code. However, it throws an error, and I can't figure out why. I can get the catalog without issue, but whenever I attempt to set stock it fails. I've tested the JSON output and it's valid.
The code I'm using:
// UPDATE Stock
//Use above token into header
$headers = array("Authorization: Bearer $token","Content-Type: application/json");
$skus = array(
'67462335058' => 8
);
foreach ($skus as $sku => $stock) {
$requestUrl='http://testing.mysite.com/index.php/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array(
"qty" => $stock
);
$productData = json_encode(array('stockItem' => $sampleProductData));
//var_dump($productData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
unset($productData);
unset($sampleProductData);
}
And get the following error:
string(1999) "{"message":"Unable to save Stock Item","trace":"#0 /home/mulltest/public_html/vendor/magento/module-catalog-inventory/Model/StockRegistry.php(180): Magento\CatalogInventory\Model\Stock\StockItemRepository->save(Object(Magento\CatalogInventory\Model\Stock\Item))\n#1 [internal function]: Magento\CatalogInventory\Model\StockRegistry->updateStockItemBySku('67462335058', Object(Magento\CatalogInventory\Model\Stock\Item))\n#2 /home/mulltest/public_html/vendor/magento/module-webapi/Controller/Rest.php(330): call_user_func_array(Array, Array)\n#3 /home/mulltest/public_html/vendor/magento/module-webapi/Controller/Rest.php(239): Magento\Webapi\Controller\Rest->processApiRequest()\n#4 /home/mulltest/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))\n#5 /home/mulltest/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)\n#6 /home/mulltest/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))\n#7 /home/mulltest/public_html/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(26): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)\n#8 /home/mulltest/public_html/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))\n#9 /home/mulltest/public_html/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http->launch()\n#10 /home/mulltest/public_html/index.php(57): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))\n#11 {main}"}"
Any ideas? Thank you!
Hey @ryan_shenk
Check that you are using same process in your Code.
Use PUT method on
http://yourmagento.com/rest/V1/products/{productSku}/stockItems/{itemId}.
For example:
http://yourmagento.com/rest/V1/products/24-MB01/stockItems/1
*stockItems will be always 1 if you're not having multiple stocks.
As parameter, provide qty in JSON, for example:
{"stockItem":{"qty":100}}
Also this issues is also registered on GitHub.
Here I found some solutions, works for this.
1) Elasticsearch service was not active. After starting the elasticsearch service the error was gone
2) Had same error on a update from 2.2.3 to 2.2.4. magento indexer:reindex helped me out
I hope it will help you.
Thanks
--
If my answer is useful, please Accept as Solution & give Kudos
I had the similar issue where I was not able to save/create a new product in admin.
Running reindex on below indexers solved the issue.
php bin/magento indexer:reindex inventory catalog_product_attribute catalog_product_category
This helps. Thanks