Inheritance: Bukimedia.PrestaSharp.Entities.PrestaShopEntity
Example #1
0
        private async Task <product_option_value> GetOptionValue(string option, string colorCode, long optionId)
        {
            var filter = new Dictionary <string, string> {
                { "name", option }
            };
            var optionValue = (await _apiFactory.OptionsValueFactory.GetByFilter(filter, null, null)).FirstOrDefault();

            if (optionValue == null)
            {
                optionValue = new product_option_value
                {
                    name = new List <language> {
                        new language(1, option)
                    },
                    id_attribute_group = optionId,
                    color = colorCode,
                };
                return(await _apiFactory.OptionsValueFactory.Add(optionValue));
            }
            return(optionValue);
        }
Example #2
0
        public async Task <combination> GetOrCreateCombination(product product, Assort assort)
        {
            Dictionary <string, string> filter;
            List <combination>          combinations = null;

            if (!string.IsNullOrWhiteSpace(assort.Reference))
            {
                filter = new Dictionary <string, string> {
                    { "reference", assort.Reference }
                };
                combinations = await _apiFactory.CombinationFactory.GetByFilter(filter, null, null);
            }

            if (combinations != null && combinations.Any())
            {
                return(combinations.First());
            }

            product_option_value colorOptionValue = null;

            if (!string.IsNullOrWhiteSpace(assort.Color))
            {
                colorOptionValue = await GetOptionValue(assort.Color, assort.ColorCode, _apiFactory.ColorOption.id.Value);
            }

            product_option_value sizeOptionValue = null;

            if (!string.IsNullOrWhiteSpace(assort.Size))
            {
                sizeOptionValue = await GetOptionValue(assort.Size, string.Empty, _apiFactory.SizeOption.id.Value);
            }

            if (colorOptionValue == null && sizeOptionValue == null)
            {
                return(null);
            }

            filter = new Dictionary <string, string> {
                { "id_product", product.id.Value.ToString() }
            };
            combinations = await _apiFactory.CombinationFactory.GetByFilter(filter, null, null);

            if (combinations == null || !combinations.Any())
            {
                return(await CreateCombination(product, assort, sizeOptionValue, colorOptionValue, true));
            }

            foreach (var combination in combinations)
            {
                if (sizeOptionValue != null && colorOptionValue != null)
                {
                    if (combination.associations.product_option_values.Exists(s => s.id == sizeOptionValue.id.Value) &&
                        combination.associations.product_option_values.Exists(s => s.id == colorOptionValue.id.Value))
                    {
                        return(await CheckCombination(combination, assort, product));
                    }
                }
                else if (colorOptionValue != null)
                {
                    if (combination.associations.product_option_values.Exists(s => s.id == colorOptionValue.id.Value))
                    {
                        return(await CheckCombination(combination, assort, product));
                    }
                }
                else
                {
                    if (combination.associations.product_option_values.Exists(s => s.id == sizeOptionValue.id.Value))
                    {
                        return(await CheckCombination(combination, assort, product));
                    }
                }
            }

            return(await CreateCombination(product, assort, sizeOptionValue, colorOptionValue, false));
        }
 private product_option_value GetOptionValue(string option, string colorCode, long optionId)
 {
     var filter = new Dictionary<string, string> { { "name", option } };
     var optionValue = _apiFactory.OptionsValueFactory.GetByFilter(filter, null, null).FirstOrDefault();
     if (optionValue == null)
     {
         optionValue = new product_option_value
         {
             name = new List<language> { new language(1, option) },
             id_attribute_group = optionId,
             color = colorCode,
         };
         return _apiFactory.OptionsValueFactory.Add(optionValue);
     }
     return optionValue;
 }
Example #4
0
        private async Task <combination> CreateCombination(product product, Assort assort, product_option_value sizeOptionValue, product_option_value colorOptionValue, bool isDefault)
        {
            var combination = new combination
            {
                id_product       = product.id,
                reference        = assort.Reference,
                ean13            = assort.Ean13,
                associations     = new AssociationsCombination(),
                minimal_quantity = 1,
                default_on       = Convert.ToInt32(isDefault),
            };

            combination.associations.product_option_values = new List <Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value>();
            if (colorOptionValue != null)
            {
                combination.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value {
                    id = colorOptionValue.id.Value
                });
            }
            if (sizeOptionValue != null)
            {
                combination.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value {
                    id = sizeOptionValue.id.Value
                });
            }

            combination = await _apiFactory.CombinationFactory.Add(combination);

            product = ProductsMapper.MapCombination(product, combination);

            Log.Information("Combitation created. Size: {0}, Color: {1}. Product reference: {2}, Combination Reference: {3}",
                            sizeOptionValue?.name[0].Value,
                            colorOptionValue?.name[0].Value,
                            product.reference,
                            combination.reference);

            await _apiFactory.ProductFactory.Update(product);

            return(combination);
        }
        private combination CreateCombination(product product, Assort assort, product_option_value sizeOptionValue, product_option_value colorOptionValue, bool isDefault)
        {
            var combination = new combination
            {
                id_product = product.id,
                reference = assort.Reference,
                ean13 = assort.Ean13,
                associations = new AssociationsCombination(),
                minimal_quantity = 1,
                default_on = Convert.ToInt32(isDefault),
            };
            combination.associations.product_option_values = new List<Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value>();
            if (colorOptionValue != null)
            {
                combination.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value { id = colorOptionValue.id.Value });
            }
            if(sizeOptionValue != null)
            {
                combination.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value { id = sizeOptionValue.id.Value });
            }

            combination = _apiFactory.CombinationFactory.Add(combination);
            product = ProductsMapper.MapCombination(product, combination);
            _apiFactory.ProductFactory.Update(product);
            return combination;
        }