Jump to content

Webservice : How To Set Product Quantity ?


miaoumix

Recommended Posts

Hi all,

 

I'm relatively new to prestashop and tried to deal with webservices.

At the moment, I can create categories, products and other stuff through webservices.

 

But I'm stuck to setting product quantity. It seams I need to deal with the stock_availables webservice, but it always refuses to update the quantity I set.

 

What I did till now was :

  • Create product, and get the stock_availables id
  • use a stock_availables resource, set quantity, and (try to) modify it through webservice. <= this doesn't work.

Would someone know how to do this ?

 

Thanks in advance,

 

Jim

Edited by miaoumix (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...

To set the amount of the product must be done separately

1) Get the field ​​id and attribute_id from prefix_stock_available table for the product and an id for each combination.

2) After get the attribute_id go and have to change their values

I copy two functions for this task and called after creating the product

 

After add product:

getIdStockAvailableAndSet($xml->product->id);

 

 

function set_product_quantity($ProductId, $StokId, $AttributeId){
global $webService;
$xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/stock_availables?schema=blank'));
$resources = $xml -> children() -> children();
$resources->id = $StokId;
$resources->id_product  = $ProductId;
$resources->quantity = 10000000;
$resources->id_shop = 1;
$resources->out_of_stock=1;
$resources->depends_on_stock = 0;
$resources->id_product_attribute=$AttributeId;
try {
	$opt = array('resource' => 'stock_availables');
	$opt['putXml'] = $xml->asXML();
	$opt['id'] = $StokId ;
	$xml = $webService->edit($opt);
}catch (PrestaShopWebserviceException $ex) {
	echo "<b>Error al setear la cantidad  ->Error : </b>".$ex->getMessage().'<br>';
}
}

 

 

 

function getIdStockAvailableAndSet($ProductId){
global $webService;
$opt['resource'] = 'products';
$opt['id'] = $ProductId;
$xml = $webService->get($opt);
foreach ($xml->product->associations->stock_availables->stock_available as $item) {
   //echo "ID: ".$item->id."<br>";
   //echo "Id Attribute: ".$item->id_product_attribute."<br>";
   set_product_quantity($ProductId, $item->id, $item->id_product_attribute);
}
}

Edited by enovo (see edit history)
  • Like 3
Link to comment
Share on other sites

  • 8 months later...
  • 1 month later...

$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

$opt = array('resource' => 'products');
$opt['postXml'] = $xml -> asXML();

$xml = $webService -> add($opt);

$ProductId = $xml->product->id;

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hi all,

i've tried to use the code above to set quantity of my product but without success..

What i've wrongly managed ?

 

 

<?php
define('DEBUG', true);
define('_PS_DEBUG_SQL_', true);
define('PS_SHOP_PATH', 'http://ZZZZZZZ.ZZZ'); // Root path of your PrestaShop store
define('PS_WS_AUTH_KEY', 'ZZZZZZZZZZZZZZZZZZZZZZZZ'); // Auth key (Get it in your Back Office)
require_once('./PSWebServiceLibrary.php');
 
getIdStockAvailableAndSet(1);
 
function set_product_quantity($ProductId, $StokId, $AttributeId){
global $webService;
$xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/stock_availables?schema=blank'));
$resources = $xml -> children() -> children();
$resources->id = $StokId;
$resources->id_product  = $ProductId;
$resources->quantity = 10000000;
$resources->id_shop = 1;
$resources->out_of_stock=1;
$resources->depends_on_stock = 0;
$resources->id_product_attribute=$AttributeId;
try {
$opt = array('resource' => 'stock_availables');
$opt['putXml'] = $xml->asXML();
$opt['id'] = $StokId ;
$xml = $webService->edit($opt);
}catch (PrestaShopWebserviceException $ex) {
echo "<b>Error al setear la cantidad  ->Error : </b>".$ex->getMessage().'<br>';
}
}
 
function getIdStockAvailableAndSet($ProductId){
global $webService;
$opt['resource'] = 'products';
$opt['id'] = $ProductId;
$xml = $webService->get($opt);
foreach ($xml->product->associations->stock_availables->stock_available as $item) {
  //echo "ID: ".$item->id."<br>";
  //echo "Id Attribute: ".$item->id_product_attribute."<br>";
  set_product_quantity($ProductId, $item->id, $item->id_product_attribute);
}
}
 
 
?>
 
  • Like 1
Link to comment
Share on other sites

  • 8 months later...
  • 2 months later...
  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...