Juanito_ Posted October 15, 2020 Share Posted October 15, 2020 Hi, I found out there is a method to do stock update on multiple product with only one request. Maybe some of you already now the way but I didnt found it when I was searsching for. Solution : 1) Create a web request to "http://your.shop.url/api/stock_availables/1&display=[id,quantity]&io_format=JSON" with your optional credentials. 2) Use method="PUT" and ContentType="application/xml" 3) Add the following XML content : "<prestashop>\n" + " <stock_available>\n" + " <id>" + id_prod1 + "</id>\n" + " <id_product>" + id_prod1 + "</id_product>\n" + " <id_product_attribute>0</id_product_attribute>\n" + " <id_shop>1</id_shop>\n" + " <id_shop_group>0</id_shop_group>\n" + " <quantity>" + quantite1 + "</quantity>\n" + " <depends_on_stock>0</depends_on_stock>\n" + " <out_of_stock>" + (quantite1 == 0 ? 2 : 0) + "</out_of_stock>\n" + " <location></location>\n" + " </stock_available>\n" + " <stock_available>\n" + " <id>" + id_prod2 + "</id>\n" + " <id_product>" + id_prod2 + "</id_product>\n" + " <id_product_attribute>0</id_product_attribute>\n" + " <id_shop>1</id_shop>\n" + " <id_shop_group>0</id_shop_group>\n" + " <quantity>" + quantite2 + "</quantity>\n" + " <depends_on_stock>0</depends_on_stock>\n" + " <out_of_stock>" + (quantite2 == 0 ? 2 : 0) + "</out_of_stock>\n" + " <location></location>\n" + " </stock_available>\n" + "</prestashop>"; Or create it with a loop : private static string RequestXML(List<MyJson.Produit> list) { var xml = "<prestashop>\n"; foreach (var prod in list) { xml += " <stock_available>\n" + " <id>" + prod.web_id + "</id>\n" + " <id_product>" + prod.web_id + "</id_product>\n" + " <id_product_attribute>0</id_product_attribute>\n" + " <id_shop>1</id_shop>\n" + " <id_shop_group>0</id_shop_group>\n" + " <quantity>" + prod.quantite_commande + "</quantity>\n" + " <depends_on_stock>0</depends_on_stock>\n" + " <out_of_stock>" + (prod.quantite_commande == 0 ? 2 : 0) + "</out_of_stock>\n" + " <location></location>\n" + " </stock_available>\n"; } return xml+"</prestashop>"; } 4) Send the request and wait for response. 5) Split the string response and check if every product's stock as been updated. Performance : Using one request per product's stock 5 stock updated per second Using one request for all the product's stock : From <1second to up to 10 seconds depending on the numbrer of stocks to update With a good server and good connection. Before that, I was sending 250+ requests in 90 seconds to sync web stock with real stock in the shop. I really hope this will help someone. 1 Link to comment Share on other sites More sharing options...
VMARTINEZTARIN Posted February 15, 2023 Share Posted February 15, 2023 Hi Juanito, i speak Spanish, say you because you speak. I read your topic, and i don't understand well. I have the xml file, i read with php file the xml to update, but don't functionally. when you say "...Use method="PUT" and ContentType="application/xml"...." what means? Thanks My PHP: <?php ini_set('display_errors', 1); $myXMLfile= 'http://xxxxxxxxxxxx.com/file/stock_available.xml'; $xml = simplexml_load_file($myXMLfile); require_once('PSWebServiceLibrary.php'); define('DEBUG', true); define('PS_SHOP_PATH', 'http://xxxxxxxxxxxxx.com'); define('PS_WS_AUTH_KEY', 'WIN3Xxxxxxxxxxxxxxxxxxxxxxxxxx1S'); try { $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); $opt['resource'] = 'stock_availables'; $opt['putXml'] = $xml->asXML(); $xml = $webService->edit($opt); echo "Successfully updated."; } catch (PrestaShopWebserviceException $ex) { $trace = $ex->getTrace(); if ($trace[0]['args'][0] == 404) echo 'Bad ID'; else if ($trace[0]['args'][0] == 401) echo 'Bad auth key'; else echo 'Other error<br />'.$ex->getMessage(); } ?> My XML:<?xml version="1.0" encoding="utf-8"?> <prestashop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <stock_available> <id><![CDATA[257]]></id> <id_product><![CDATA[80]]></id_product> <id_product_attribute><![CDATA[228]]></id_product_attribute> <id_shop><![CDATA[1]]></id_shop> <id_shop_group><![CDATA[1]]></id_shop_group> <quantity><![CDATA[100]]></quantity> <depends_on_stock><![CDATA[0]]></depends_on_stock> </stock_available> </prestashop> Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now