Jump to content

Update stock via webservice in multi-store


setecg

Recommended Posts

Hi!

I'm trying to update the stock of the products via API/webservice.

I can update the stock in a single-store Prestashop, and even in a multi-store prestashop I created for testing purposes. However I can update the stocks in a customer's shop. I can update any other product data but the stock.

I will explain this with an specific example:

There are two stores: one with ID=1 and another one with ID=4.

The Webservice returns this product info.:

https://myshop.com/api/products/999

In the obtained info, I see the stock_svailables node with this info:

id = 12345

id_product_attribute = 0

id_shop_default = 4

So I can now get the stocks_available for that product:

https://mitienda.com/api/stock_availables/12345

id_shop: 0

quantity: 4

etc...

I don't understand why product.id_shop_default is 4 but stocks_available.id_shop is 0. However, I insist...

So, if I want to modify the stock, I send this XML to stocks_available/12345:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<stock_available>
	<id>12345</id>
	<id_product>999</id_product>
	<id_product_attribute>0</id_product_attribute>
	<id_shop>0</id_shop>
	<id_shop_group></id_shop_group>
	<quantity>8</quantity>
	<depends_on_stock>0</depends_on_stock>
	<out_of_stock>2</out_of_stock>
	<location></location>
</stock_available>
</prestashop>

I have tried specifying id_shop, id_group, or leaving them empty. Same result: product stock doesn't update.

No error message in the request. Result is OK - 200

The request is executed correctly, but the stock in Prestashop is not changed.

If I go ask for stocks_available/12345, I get something like 'there's not stocks_available for this product in this shop', and api/products/999 returns now an empty stocks_available node.

I don't know if the problem is in my customer's Prestashop settings, or if he has products created in Shop 1 but stock in Shop 4, etc., but if I replicate this in my testing shop, I can update the stock with no problem.

Dooes anybody know if I have to do something different when updating stock in multi-store?

Thanks!

Link to comment
Share on other sites

  • 2 years later...
$this->api = 'XXX';
$this->link = 'https://fyazilim.com';
$id = 100; // ID Prestashop product
$idStok = 571; // ID Stock aviable
$stock = 10;
$xmlData = <<<XML
			<prestashop>
			<stock_available xmlns:xlink="http://www.w3.org/1999/xlink">
				<id><![CDATA[$idStok]]></id>
				<id_product xlink:href="$this->api/api/products/$id"><![CDATA[$id]]></id_product>
				<id_product_attribute><![CDATA[0]]></id_product_attribute>
				<id_shop xlink:href="$this->api/api/shops/1"><![CDATA[1]]></id_shop>
				<id_shop_group><![CDATA[0]]></id_shop_group>
				<quantity><![CDATA[$stock]]></quantity>
				<depends_on_stock><![CDATA[0]]></depends_on_stock>
				<out_of_stock><![CDATA[2]]></out_of_stock>
				<location></location>
			</stock_available>
			</prestashop>
			XML;

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $updateUrl);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
			curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
			curl_setopt($ch, CURLOPT_USERPWD, $this->api . ':');
			curl_setopt($ch, CURLOPT_HTTPHEADER, [
				'Content-Type: application/xml',
			]);

			$response = curl_exec($ch);

			if (!$response) {
				echo "CURL Hatası: " . curl_error($ch);
				curl_close($ch);
				return;
			}

			curl_close($ch);

			echo "Response: " . htmlentities($response);

 

Link to comment
Share on other sites

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...