Skip to content

Instantly share code, notes, and snippets.

@udovicic
Created October 31, 2015 11:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udovicic/c65e2cef30c45e044b1d to your computer and use it in GitHub Desktop.
Save udovicic/c65e2cef30c45e044b1d to your computer and use it in GitHub Desktop.
SOAP v2 Inventory update example
<?php
/**
* Magento catalogInventoryStockItemUpdate API example Using SOAPv2
*
* Local measurements:
* Login time: 1.3139259815216s
* Total execution time: 34.751662969589s
*/
/**
* Constant definitions
*/
$apiUrlV2 = "http://domain.loc/api/v2_soap/?wsdl=1";
$username = 'test';
$password = 'abc123';
/**
* Array containing sample data
*
* In this context, 'is_in_stock' is optional. It is used for explicitly setting
* value. Otherwise, "Qty for Item's Status to Become Out of Stock" will
* be used for determining stock status
*/
$sampleData = [
['id' => 267702, 'qty' => 5, 'is_in_stock' => 1],
['id' => 267701, 'qty' => 6, 'is_in_stock' => 1],
['id' => 267700, 'qty' => 7, 'is_in_stock' => 1],
['id' => 267699, 'qty' => 8, 'is_in_stock' => 1],
['id' => 267663, 'qty' => 9, 'is_in_stock' => 1],
['id' => 267662, 'qty' => 1, 'is_in_stock' => 1],
['id' => 267661, 'qty' => 2, 'is_in_stock' => 1],
['id' => 267660, 'qty' => 3, 'is_in_stock' => 1],
['id' => 267659, 'qty' => 4, 'is_in_stock' => 1],
['id' => 267658, 'qty' => 5, 'is_in_stock' => 1],
['id' => 267657, 'qty' => 13, 'is_in_stock' => 1],
['id' => 267656, 'qty' => 15, 'is_in_stock' => 1],
['id' => 267655, 'qty' => 88, 'is_in_stock' => 1],
['id' => 267654, 'qty' => 99, 'is_in_stock' => 1],
['id' => 267653, 'qty' => 33, 'is_in_stock' => 1],
['id' => 267652, 'qty' => 43, 'is_in_stock' => 1],
['id' => 267651, 'qty' => 23, 'is_in_stock' => 1],
['id' => 267650, 'qty' => 0, 'is_in_stock' => 0],
['id' => 267649, 'qty' => 1, 'is_in_stock' => 1],
['id' => 267648, 'qty' => 53, 'is_in_stock' => 1]
];
$timeStart = microtime(true); // DEBUG
// Create soap client and login to system.
// Reuse sessionID for multiple API calls if possible!!
$soap = new SoapClient($apiUrlV2);
$sessionId = $soap->login($username, $password);
$timeLogin = microtime(true) - $timeStart; // DEBUG
echo "Login time: {$timeLogin}s\n"; // DEBUG
// Submit API calls
foreach($sampleData as $item) {
$result = $soap->catalogInventoryStockItemUpdate(
$sessionId,
$item['id'],
[
'qty' => $item['qty'],
'is_in_stock' => $item['is_in_stock'] // Optional
]
);
}
$timeEnd = microtime(true) - $timeStart; // DEBUG
echo "Execution time: {$timeEnd}s\n"; // DEBUG
<?php
/**
* Magento catalogInventoryStockItemMultiUpdate API example Using SOAPv2
*
* Local measurements:
* Login time: 1.3783569335938s
* Total execution time: 2.8101768493652s
*/
/**
* Constant definitions
*/
$apiUrlV2 = "http://domain.loc/api/v2_soap/?wsdl=1";
$username = 'test';
$password = 'abc123';
/**
* Array containing sample data
*
* In this context, 'is_in_stock' is optional. However, performing multi update,
* will not affect is_in_stock automatically. If you wish to adjust is_in_stock
* status, you will have to send that info as well.
*/
$sampleData = [
['id' => 267702, 'qty' => 5, 'is_in_stock' => 1],
['id' => 267701, 'qty' => 6, 'is_in_stock' => 1],
['id' => 267700, 'qty' => 7, 'is_in_stock' => 1],
['id' => 267699, 'qty' => 8, 'is_in_stock' => 1],
['id' => 267663, 'qty' => 9, 'is_in_stock' => 1],
['id' => 267662, 'qty' => 1, 'is_in_stock' => 1],
['id' => 267661, 'qty' => 2, 'is_in_stock' => 1],
['id' => 267660, 'qty' => 3, 'is_in_stock' => 1],
['id' => 267659, 'qty' => 4, 'is_in_stock' => 1],
['id' => 267658, 'qty' => 5, 'is_in_stock' => 1],
['id' => 267657, 'qty' => 13, 'is_in_stock' => 1],
['id' => 267656, 'qty' => 15, 'is_in_stock' => 1],
['id' => 267655, 'qty' => 88, 'is_in_stock' => 1],
['id' => 267654, 'qty' => 99, 'is_in_stock' => 1],
['id' => 267653, 'qty' => 33, 'is_in_stock' => 1],
['id' => 267652, 'qty' => 43, 'is_in_stock' => 1],
['id' => 267651, 'qty' => 23, 'is_in_stock' => 1],
['id' => 267650, 'qty' => 0, 'is_in_stock' => 0],
['id' => 267649, 'qty' => 1, 'is_in_stock' => 1],
['id' => 267648, 'qty' => 53, 'is_in_stock' => 1]
];
$timeStart = microtime(true); // DEBUG
// Create soap client and login to system.
// Reuse sessionID for multiple API calls if possible!!
$soap = new SoapClient($apiUrlV2);
$sessionId = $soap->login($username, $password);
$timeLogin = microtime(true) - $timeStart; // DEBUG
echo "Login time: {$timeLogin}s\n"; // DEBUG
// Prepare data
$ids = [];
$data = [];
foreach ($sampleData as $item) {
$ids[] = $item['id'];
$data[] = [
'qty' => $item['qty'],
'is_in_stock' => $item['is_in_stock'] // This is optional, but see notes
];
}
// Submit API call
$result = $soap->catalogInventoryStockItemMultiUpdate(
$sessionId,
$ids,
$data
);
$timeEnd = microtime(true) - $timeStart; // DEBUG
echo "Total execution time: {$timeEnd}s\n"; // DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment