Bukimedia / PrestaSharp

CSharp .Net client library for the PrestaShop API via web service
GNU General Public License v3.0
154 stars 151 forks source link

Associations for Combination example. #149

Closed RCallejo2 closed 9 years ago

RCallejo2 commented 9 years ago

I would be very gratefull if someone could send me an Combination code example including any Association for the combination.

Thanks in advance.

EduLeonPavon commented 9 years ago

Hello @RCallejo2 You can guide with this method. Regards.

        private void AddPrestaSharpCombination(Bukisync.Common.Entities.Core.combination combination, Bukimedia.PrestaSharp.Entities.product PrestaSharpProduct, Bukisync.Common.Entities.Core.product product, int StockAvailableIndex, Bukimedia.PrestaSharp.Entities.combination PrestaSharpCombination)
        {
            bool flag = true;
            while (flag)
            {
                try
                {
                    this.CatalogMemory.Logger.LogInfoStampNewLine("Añadiendo combinación y stock de combinación. Referencia: " + combination.reference + " .......................... iniciado");
                    lock (this.CombinationFactory)
                    {
                        PrestaSharpCombination = this.CombinationFactory.Add(PrestaSharpCombination);
                    }
                    flag = false;
                    //Actualización de la cantidad de stockaje del objeto combination que acabamos de crear
                    bool flag2 = true;
                    while (flag2)
                    {
                        try
                        {
                            PrestaSharpProduct = RefreshPrestaSharpProduct(PrestaSharpProduct);
                            Bukimedia.PrestaSharp.Entities.stock_available StockAvailable = this.StockAvailableFactory.Get((long)PrestaSharpProduct.associations.stock_availables[StockAvailableIndex + 1].id);
                            StockAvailable.quantity = combination.quantity;
                            StockAvailable.out_of_stock = combination.out_of_stock ? 1 : 0;
                            StockAvailable.depends_on_stock = combination.depends_of_stock ? 1 : 0;
                            lock (this.StockAvailableFactory)
                            {
                                this.StockAvailableFactory.Update(StockAvailable);
                            }
                            this.CatalogMemory.Logger.LogInfoStampNewLine("Añadiendo combinación y stock de combinación. Referencia: " + combination.reference + " .......................... completado");
                            flag2 = false;
                            this.AddPrestaSharpCombinationImages(PrestaSharpProduct, combination, PrestaSharpCombination);
                        }
                        catch (System.ArgumentOutOfRangeException ex)
                        {
                            string AuxMessage = "";
                            AuxMessage += "PrestaSharpProduct.reference: " + PrestaSharpProduct.reference;
                            AuxMessage += "combination.reference: " + combination.reference;
                            AuxMessage += "i: " + StockAvailableIndex;
                            AuxMessage += "PrestaSharpProduct.associations.stock_availables.Count(): " + PrestaSharpProduct.associations.stock_availables.Count();
                            this.CatalogMemory.Logger.LogErrorStampNewLine("Se produjo un error con la conexión a PrestaShop. Se reintentará la conexión en 5 minutos. " + AuxMessage + Environment.NewLine + ex.ToString());
                            this.CatalogMemory.Logger.LogDebugStampNewLine(ex.ToString());
                            Thread.Sleep(300000);
                        }
                        catch (Exception ex)
                        {
                            this.CatalogMemory.Logger.LogErrorStampNewLine("Se produjo un error con la conexión a PrestaShop. Se reintentará la conexión en 5 minutos. " + Environment.NewLine + ex.ToString());
                            this.CatalogMemory.Logger.LogDebugStampNewLine(ex.ToString());
                            Thread.Sleep(300000);
                        }
                    }
                }
                catch (ApplicationException ex)
                {
                    flag = false;
                    ex.ToString();
                    //Actualización de la cantidad de stockaje del objeto combination que acabamos de crear
                    bool flag2 = true;
                    while (flag2)
                    {
                        try
                        {
                            PrestaSharpProduct = this.GetPrestaSharpProduct((long)PrestaSharpProduct.id);
                            Bukimedia.PrestaSharp.Entities.stock_available StockAvailable = this.StockAvailableFactory.Get((long)PrestaSharpProduct.associations.stock_availables[StockAvailableIndex + 1].id);
                            StockAvailable.quantity = product.quantity;
                            StockAvailable.out_of_stock = combination.out_of_stock ? 1 : 0;
                            StockAvailable.depends_on_stock = combination.depends_of_stock ? 1 : 0;
                            lock (this.StockAvailableFactory)
                            {
                                this.StockAvailableFactory.Update(StockAvailable);
                            }
                            this.CatalogMemory.Logger.LogInfoStampNewLine("Añadiendo combinación y stock de combinación. Referencia: " + combination.reference + " .......................... completado");
                            flag2 = false;
                        }
                        catch (Exception ex2)
                        {
                            this.CatalogMemory.Logger.LogErrorStampNewLine("Se produjo un error con la conexión a PrestaShop. Se reintentará la conexión en 5 minutos. " + Environment.NewLine + ex2.ToString());
                            this.CatalogMemory.Logger.LogDebugStampNewLine(ex.ToString());
                            Thread.Sleep(300000);
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.CatalogMemory.Logger.LogErrorStampNewLine("Se produjo un error con la conexión a PrestaShop. Se reintentará la conexión en 5 minutos. " + Environment.NewLine + ex.ToString());
                    this.CatalogMemory.Logger.LogDebugStampNewLine(ex.ToString());
                    Thread.Sleep(300000);
                }
            }
        }
RCallejo2 commented 9 years ago

Thanks a lot Edu for your support.

The code you sent has a lots of things to learn from. Unfortunately, my expertise goes far away from your example and I do not know if I will be able to understand it but I´ll try. I will appreciate a couple of small examples of how to: 1.- Create a combination (Size,Color) and "associate" it to the product. 1.- Create an association (Categories of a product) and how to "associate" it to the product.

Surely I am too "green" and I need also to start learning Prestashop internals..Is it there any good manual or technical paper available??

I appreciate very much your support.

Digitaldot-Murcia commented 8 years ago

Hello i like set a example, of how create an combination product in prestashop with prestasharpt, I hope it is of interest.

CombinationFactory CombinationFactory = new CombinationFactory(BaseUrl, Account, Password); Bukimedia.PrestaSharp.Entities.combination Combination = new Bukimedia.PrestaSharp.Entities.combination(); Dictionary<string, string> dtn = new Dictionary<string, string>(); dtn.Add("reference", referer);

Bukimedia.PrestaSharp.Entities.combination fabricanteobject = new Bukimedia.PrestaSharp.Entities.combination(); List combinationexist = CombinationFactory.GetByFilter(dtn, null, null);

Combination.id_product = idproduct; Combination.quantity = quantity; Combination.reference = referer; Combination.minimal_quantity = minimaunidad; Combination.unit_price_impact = impactoprecio;

List productvalue = new List(); productvalue.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value()); if (colour != " ") { int idcombinacion = check_combination(1, colour, 1); productvalue[0].id = Convert.ToInt32(idcombinacion); Combination.associations.product_option_values.Add(productvalue[0]); Combination.quantity = quantity; } try { CombinationFactory.Add(Combination); } catch{}

Bukimedia.PrestaSharp.Entities.combination fabricanteobject2 = new Bukimedia.PrestaSharp.Entities.combination(); combinationexist = CombinationFactory.GetByFilter(dtn, null, null); if (size != " ") { int combinacion = check_combination(1, size, 1); productvalue[0].id = Convert.ToInt32(combinacion); combinationexist[0].associations.product_option_values.Add(productvalue[0]); combinationexist[0].quantity = quantity; combinationexist[0].id_product = idproduct; combinationexist[0].quantity = quantity; combinationexist[0].minimal_quantity = minimaunidad; combinationexist[0].unit_price_impact = impactoprecio; combinationexist.Add(combinationexist[0]); } try { CombinationFactory.Update(combinationexist[0]); } catch{}

ccarabelli commented 8 years ago

Hello @Digitaldot-Murcia , i have any questions.

1- I can not find the function check_combination(1,x,1) 2- dtn.Add("reference", referer); referer????

Thanks for your help!!!

Digitaldot-Murcia commented 8 years ago

Hi, the funtion check_combination give 0 if not exist and else return id of combination.

Referer is the reference for my combination.

static Int32 check_combination(int idlanguage, string valueofcombination, int grupo) { Int32 idcombinacion = 0; ProductOptionValueFactory ProductOptionValueFactory = new ProductOptionValueFactory(BaseUrl, Account, Password); Dictionary<string, string> dtnbuscarvalueofcombination = new Dictionary<string, string>(); dtnbuscarvalueofcombination.Add("name", valueofcombination); List currentidvalueattribute = ProductOptionValueFactory.GetByFilter(dtnbuscarvalueofcombination, null, null); Bukimedia.PrestaSharp.Entities.product_option_value povalues = new Bukimedia.PrestaSharp.Entities.product_option_value(); if (currentidvalueattribute.Count > 0) { try { currentidvalueattribute[0].name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language(idlanguage, valueofcombination)); ProductOptionValueFactory.Update(currentidvalueattribute[0]); idcombinacion = Convert.ToInt32(currentidvalueattribute[0].id.ToString().Replace(" ", "")); } catch { //Exist } } else { try { povalues.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language(idlanguage, valueofcombination)); povalues.id_attribute_group = grupo; povalues = ProductOptionValueFactory.Add(povalues); currentidvalueattribute = ProductOptionValueFactory.GetByFilter(dtnbuscarvalueofcombination, null, null); idcombinacion = Convert.ToInt32(currentidvalueattribute[0].id.ToString().Replace(" ", "")); } catch { //Exist } }

RCallejo2 commented 8 years ago

Thank you all for your so invaluable contributions !!!

I have the next issue:

I create a product -> ok. I create several combinations -> Everything seems to be ok.

When I go to the prestashop frontend, in the product description, I see that the price corresponds to the "unit_price_impact" value and not to the "base_price + unit_price_impact" as it should be. If I go to the backEnd and I just save one of the combinations (whichever!!), all prices and combinations are showing right prices.

Is this happening to you too?. Is This a problem of RestSharp, Prestasharp or Prestashop webservices? In my oppinion it is a problem of Prestashop. Could you please investigate about that so we can open an issue in prestashop support?

Thanks in advance.

I attach a couple of screeshots of before and after. before updating combination after updating combination

EduLeonPavon commented 8 years ago

Hello @RCallejo2 Have you marked a combination as default?

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.