0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.