Jump to content

c# & webservice : when I update a product, my features value are deleted


Runan

Recommended Posts

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 :angry:

 

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

  • 2 weeks later...

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 by Runan (see edit history)
Link to comment
Share on other sites

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 by m33ts4k0z (see edit history)
Link to comment
Share on other sites

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

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 by m33ts4k0z (see edit history)
Link to comment
Share on other sites

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 :unsure: , 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

  • 1 month 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...