Skip to content

Instantly share code, notes, and snippets.

@titodevera
Created May 24, 2017 16:58
Show Gist options
  • Save titodevera/4a873dbb3f97d2fbff382bc18496a918 to your computer and use it in GitHub Desktop.
Save titodevera/4a873dbb3f97d2fbff382bc18496a918 to your computer and use it in GitHub Desktop.
Prestashop Webservice helpful functions
/**
* Update the stock of a product after create it through the api
* Tested on PrestaShop 1.7.1.1
*
* @link https://github.com/haka002/PrestaShop-webservice-lib PrestaShop 1.7 WebService lib
*
* @param SimpleXMLElement $product
* @param int $quantity
*
* @return void
*/
function set_product_quantity( $product, $quantity ){
try{
$webservice_client = new PrestaShopWebservice('http://localhost/ps', 'apikeygoeshere', true);
foreach( $product->product->associations->stock_availables->stock_available as $item ){
$xml = $webservice_client->get( array('url' => 'http://localhost/ps/api/stock_availables?schema=blank') );
$resources = $xml->children()->children();
$resources->id = $item->id;
$resources->id_product = $product->product->id;
$resources->quantity = $quantity;
$resources->id_shop = 1;
$resources->out_of_stock = 1;
$resources->depends_on_stock = 0;
$resources->id_product_attribute = $item->id_product_attribute;
$xml = $webservice_client->edit(array(
'resource' => 'stock_availables',
'putXml' => $xml->asXML(),
'id' => $item->id
));
}
}catch( PrestaShopWebserviceException $e ){
die($e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment