Runan Posted March 8, 2013 Share Posted March 8, 2013 Hello, I interact whith my Prestahsop 1.5.2 with my c# program. I manage to update prices of my products but all features value of the updated products are deleted When I inspect with fiddler 2 the data of the "put" contains features value of the product. Any idea of the reason ? Pleasssssssse Link to comment Share on other sites More sharing options...
Runan Posted March 22, 2013 Author Share Posted March 22, 2013 Up. Link to comment Share on other sites More sharing options...
Runan Posted March 25, 2013 Author Share Posted March 25, 2013 (edited) I knew have to find a solution but not ... Edited March 26, 2013 by Runan (see edit history) Link to comment Share on other sites More sharing options...
Runan Posted March 26, 2013 Author Share Posted March 26, 2013 I am not alone http://www.amazon.fr/Assassins-Creed-III-édition-bonus/dp/B0079YORAY/?_encodingEEGGAALLUTF8ETCOMMERCIALtagEEGGAALLpregam-21ETCOMMERCIALlinkCodeEEGGAALLur2ETCOMMERCIALcampEEGGAALL1642ETCOMMERCIALcreativeEEGGAALL6746 Link to comment Share on other sites More sharing options...
l3msip Posted March 26, 2013 Share Posted March 26, 2013 Care to share your c# client code? Impossible to help otherwise. I am also a c# dev and am just starting on a prestashop 1.5.3 client app, so maybe i can help, but you will need to post your code. Link to comment Share on other sites More sharing options...
Runan Posted March 26, 2013 Author Share Posted March 26, 2013 (edited) You are right : public void MAJPrixProduitAmazon(int id_produit, string prix) { byte[] byteArrayMAJPrix = null; WebResponse Resp = RequetePrestashop("products/" + id_produit, "GET"); XmlNodeList ListeXml; try { XmlDocument RetourXml = new XmlDocument(); Stream dataStream = Resp.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String R = reader.ReadToEnd(); RetourXml.LoadXml(R); // On supprimer les balises non modifiables // position_in_category XmlNode childNodePosition_in_category = RetourXml.SelectSingleNode("/prestashop/product/position_in_category"); // apply your xpath here childNodePosition_in_category.ParentNode.RemoveChild(childNodePosition_in_category); // manufacturer_name XmlNode childNodeManufacturer_name = RetourXml.SelectSingleNode("/prestashop/product/manufacturer_name"); // apply your xpath here childNodeManufacturer_name.ParentNode.RemoveChild(childNodeManufacturer_name); // quantity XmlNode childNode_quantity = RetourXml.SelectSingleNode("/prestashop/product/quantity"); // apply your xpath here childNode_quantity.ParentNode.RemoveChild(childNode_quantity); ListeXml = RetourXml.SelectNodes("/prestashop/product"); // On analyse les différents noeuds de la fiche produit foreach (XmlNode node in ListeXml) { foreach (XmlNode SousNoeud in node) { if (SousNoeud.Name.ToString() == "price") { SousNoeud.InnerText = prix; } } } byteArrayMAJPrix = Encoding.UTF8.GetBytes(RetourXml.InnerXml.ToString()); reader.Close(); dataStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } // On prépare les données XML mis à jour // On envoie les données XML mis à jour via la méthode PUT WebRequest Req1 = WebRequest.Create(WebServicePrestashopUrl + "/products/" + id_produit); Req1.Method = "PUT"; Req1.Credentials = new NetworkCredential(WebServicePrestashopCle, ""); Req1.ContentLength = byteArrayMAJPrix.Length; Req1.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; //MessageBox.Show(Encoding.UTF8.GetString(byteArrayMAJPrix)); //EnvoiMail("[email protected]", "[email protected]", "En-tête XML du produit : " + id_produit.ToString(), Encoding.UTF8.GetString(byteArrayMAJPrix)); Stream postStream = null; postStream = Req1.GetRequestStream(); postStream.Write(byteArrayMAJPrix, 0, byteArrayMAJPrix.Length); postStream.Close(); HttpWebResponse Resp1 = (HttpWebResponse)Req1.GetResponse(); if (Resp1.StatusCode == HttpStatusCode.OK ) { //MessageBox.Show("Le produit (" + id_produit.ToString() + ") a été mis à jour."); // On met à jour le label } else { MessageBox.Show(Resp1.StatusCode + Resp1.StatusDescription); } } In advance, thanks for your help ! Edited March 26, 2013 by Runan (see edit history) Link to comment Share on other sites More sharing options...
m33ts4k0z Posted March 27, 2013 Share Posted March 27, 2013 (edited) When you want to edit a resource, you need to read the whole XML file of the resource by using the GET method first. Then you need to edit the values read and finally return the edited XML. If you send only the updated information then the rest of the values not included in the PUT request will be overwritten to null. I've myself developed an app like this on C# and works pretty well however using the RESTful component to upgrade thousands of products isn't ideal as it takes a great deal of time since the web server has to receive a seperate XML file of around 65 values and then parse it and write it to the database. There is very nice example over MSDN that I cant really find at the moment. However you can use XMLReader to load the xml site from prestashop and XMLDocument to parse it and edit its values. You will find good examples all over on how to do this. EDIT: Looking at your code above, you dont need to create 2x webrequests. You can retrieve the file using XMLReader directly and then with XMLDocument you can do something like RetourXml.SelectSingleNode("/prestashop/product/quantity") = 20; Then you can directly return your edited XML. Edited March 27, 2013 by m33ts4k0z (see edit history) Link to comment Share on other sites More sharing options...
Runan Posted March 28, 2013 Author Share Posted March 28, 2013 When you want to edit a resource, you need to read the whole XML file of the resource by using the GET method first. Then you need to edit the values read and finally return the edited XML. If you send only the updated information then the rest of the values not included in the PUT request will be overwritten to null. I've myself developed an app like this on C# and works pretty well however using the RESTful component to upgrade thousands of products isn't ideal as it takes a great deal of time since the web server has to receive a seperate XML file of around 65 values and then parse it and write it to the database. There is very nice example over MSDN that I cant really find at the moment. However you can use XMLReader to load the xml site from prestashop and XMLDocument to parse it and edit its values. You will find good examples all over on how to do this. EDIT: Looking at your code above, you dont need to create 2x webrequests. You can retrieve the file using XMLReader directly and then with XMLDocument you can do something like RetourXml.SelectSingleNode("/prestashop/product/quantity") = 20; Then you can directly return your edited XML. Thanks for your answer, you mean I must do only 1 request with the PUT method ? Have you got any example, because I don't see how I can do that only with 1 PUT ? Link to comment Share on other sites More sharing options...
m33ts4k0z Posted March 28, 2013 Share Posted March 28, 2013 (edited) You fetch a product like that: XmlReader productsUpdate = XmlReader.Create(prestaShopURL + "/api/products/?display=full&filter[reference]=" + articleNumber, settings); docUpdate.Load(productsUpdate); You read the product with the url directly into the XMLReader and then you load it into an XMLDocument. Then you do your PUT method as you did above. Note that in that example I use reference number as ID and thats why I choose the product that has the specific reference number. If you dont use display=full but for example only "price" then the XMLReader will get only the price and when you return the XML file with your put method all the other values will be null thus the issue you currently have. Edited March 28, 2013 by m33ts4k0z (see edit history) Link to comment Share on other sites More sharing options...
Runan Posted March 28, 2013 Author Share Posted March 28, 2013 You fetch a product like that: XmlReader productsUpdate = XmlReader.Create(prestaShopURL + "/api/products/?display=full&filter[reference]=" + articleNumber, settings); docUpdate.Load(productsUpdate); You read the product with the url directly into the XMLReader and then you load it into an XMLDocument. Then you do your PUT method as you did above. Note that in that example I use reference number as ID and thats why I choose the product that has the specific reference number. If you dont use display=full but for example only "price" then the XMLReader will get only the price and when you return the XML file with your put method all the other values will be null thus the issue you currently have. Sorry to continue , but could you give me a complete example starting with your xmlreader and finishing with your complete put request ? Link to comment Share on other sites More sharing options...
Runan Posted March 30, 2013 Author Share Posted March 30, 2013 (edited) I have tested a GET xml and a PUT without any change in data xml => my feature value disappeared Anybody else has an example of c# sharp code to update product with feature value ? Pleazzzzzzzzz Edited March 30, 2013 by Runan (see edit history) Link to comment Share on other sites More sharing options...
m33ts4k0z Posted April 2, 2013 Share Posted April 2, 2013 Hello, this is a post that I found that uses similar code as mine. This will give you a very good idea on how to do it. Link to comment Share on other sites More sharing options...
ucar Posted May 27, 2013 Share Posted May 27, 2013 Hi, this seems to be prestashop webservice bug. Here is my solution. Regards Jaroslav Knespl 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