Esempio n. 1
0
        /// <summary>
        /// Creates new instance of <see cref="Variant"/> class representing a product variant.
        /// </summary>
        /// <param name="sku"><see cref="SKUInfo"/> object representing an original Kentico variant's SKU info object from which the model is created.</param>
        /// <param name="attributes"><see cref="ProductAttributeSet"/> object representing the product options of this variant.</param>
        public Variant(SKUInfo sku, ProductAttributeSet attributes)
        {
            if (sku == null)
            {
                throw new ArgumentNullException(nameof(sku));
            }
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            VariantSKU        = sku;
            ProductAttributes = attributes;
        }
Esempio n. 2
0
    /// <summary>
    /// Generates variants asynchronous.
    /// </summary>
    /// <param name="parameter">AsyncControl parameters</param>
    private void GenerateAndSave(object parameter)
    {
        try
        {
            // Regenerate already existing variants
            if (mRegenerateVariants)
            {
                ProductAttributeSet   productAttributeSet = new ProductAttributeSet(CategorySelector.SelectedCategories.Values.Where(x => x > VariantOptionInfo.NewOption));
                List <ProductVariant> existingVariants    = VariantHelper.AddNewCategoriesToVariantsOfProduct(ProductID, productAttributeSet);

                // Use special action contexts to turn off unnecessary actions
                using (ECommerceActionContext eCommerceContext = new ECommerceActionContext())
                {
                    eCommerceContext.TouchParent            = false;
                    eCommerceContext.SetLowestPriceToParent = false;

                    existingVariants.ForEach(pVariant =>
                    {
                        AddLog(HTMLHelper.HTMLEncode(ResHelper.LocalizeString(pVariant.Variant.SKUName)) + GetString("com.variants.isredefined"));
                        VariantHelper.SetProductVariant(pVariant);
                    });
                }

                // Save variant to update parent SKULastModified a SKUPrice properties
                var lastVariant = existingVariants.LastOrDefault();
                if (lastVariant != null)
                {
                    lastVariant.Variant.Generalized.SetObject();
                }
            }

            // Generate non-existing variants
            ProductVariant productVariant = null;

            // Use special action contexts to turn off unnecessary actions
            using (ECommerceActionContext eCommerceContext = new ECommerceActionContext())
            {
                eCommerceContext.TouchParent            = false;
                eCommerceContext.SetLowestPriceToParent = false;

                foreach (DataRow row in mVariantsToGenerate)
                {
                    IEnumerable <int> options = GetAllOptions(row, NewCategories.Union(ExistingCategories));
                    productVariant = new ProductVariant(ProductID, new ProductAttributeSet(options));
                    AddLog(HTMLHelper.HTMLEncode(ResHelper.LocalizeString(productVariant.Variant.SKUName)) + GetString("com.variants.isdefined"));
                    productVariant.Set();
                }
            }

            // Save variant to update parent SKULastModified a SKUPrice properties
            if (productVariant != null)
            {
                productVariant.Variant.Generalized.SetObject();
            }
        }
        catch (Exception ex)
        {
            CurrentError = GetString("com.variant.definerror");
            EventLogProvider.LogException("Variant definition", "DEFINEVARIANT", ex);
        }
    }
    /// <summary>
    /// Generates variants into DataSet and DataTable.
    /// </summary>
    /// <returns>DataSet of generated variants</returns>
    private DataSet GenerateVariants()
    {
        List<ProductVariant> productVariantList;

        // Creating new variants and some has been already generated
        if ((NewCategories.Count > 0) && (ExistingCategories.Count > 0))
        {
            ProductAttributeSet productAttributeSet = new ProductAttributeSet(CategorySelector.SelectedCategories.Values.Where(x => x > VariantOptionInfo.NewOption));
            List<ProductVariant> oldVariants = VariantHelper.AddNewCategoriesToVariantsOfProduct(ProductID, productAttributeSet);
            productVariantList = VariantHelper.GetAllPossibleVariants(oldVariants);
        }
        else
        {
            productVariantList = VariantHelper.GetAllPossibleVariants(ProductID, NewCategories.Concat(ExistingCategories));
        }

        DataSet ds = new DataSet("VariantsDS");
        DataTable dt = new DataTable("VariantsDT");

        // Build DataTable and UniGrid structure
        foreach (int categoryId in ExistingCategories.Union(NewCategories))
        {
            OptionCategoryInfo optionCategoryInfo = mAllCategoriesOptions.Keys.FirstOrDefault(k => (ValidationHelper.GetInteger(k["CategoryId"], 0) == categoryId));

            // Add columns to DataTable
            dt.Columns.Add(new DataColumn(categoryId.ToString(), typeof(String)));

            // Add column with option category live site display name to the grid with variant preview. In case live site display name is not available option category display name is used
            if (optionCategoryInfo != null)
            {
                AddGridColumn(ResHelper.LocalizeString(optionCategoryInfo.CategoryTitle), categoryId.ToString(), "#transform: ecommerce.skuoption.SKUName");
            }
        }

        // Add RowNumber column to DataTable
        dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
        AddGridColumn("RowNumber", "RowNumber", string.Empty, true);

        // Add Exist column to DataTable
        dt.Columns.Add(new DataColumn("Exist", typeof(bool)));

        // Add Variant number column to DataTable and UniGrid
        dt.Columns.Add(new DataColumn("VariantNumber", typeof(string)));
        AddGridColumn(GetString("com.sku.skunumber"), "VariantNumber");

        // Fill DataTable
        int index = 0;
        foreach (ProductVariant productVariant in productVariantList)
        {
            DataRow dr = dt.NewRow();

            int i = 0;
            foreach (int categoryId in productVariant.ProductAttributes.CategoryIDs)
            {
                // Fill values to dynamically added column
                OptionCategoryInfo optionCategoryInfo = mAllCategoriesOptions.Keys.FirstOrDefault(k => (ValidationHelper.GetInteger(k["CategoryId"], 0) == categoryId));

                if (optionCategoryInfo != null)
                {
                    dr[optionCategoryInfo.CategoryID.ToString()] = productVariant.ProductAttributes[i].SKUID;
                    i++;
                }
            }

            dr["RowNumber"] = index;
            dr["Exist"] = productVariant.Existing;
            dr["VariantNumber"] = productVariant.Variant.SKUNumber;

            dt.Rows.Add(dr);
            index++;
        }

        ds.Tables.Add(dt);
        return ds;
    }
Esempio n. 4
0
    /// <summary>
    /// Generates variants into DataSet and DataTable.
    /// </summary>
    /// <returns>DataSet of generated variants</returns>
    private DataSet GenerateVariants()
    {
        List <ProductVariant> productVariantList;

        // Creating new variants and some has been already generated
        if ((NewCategories.Count > 0) && (ExistingCategories.Count > 0))
        {
            ProductAttributeSet   productAttributeSet = new ProductAttributeSet(CategorySelector.SelectedCategories.Values.Where(x => x > VariantOptionInfo.NewOption));
            List <ProductVariant> oldVariants         = VariantHelper.AddNewCategoriesToVariantsOfProduct(ProductID, productAttributeSet);
            productVariantList = VariantHelper.GetAllPossibleVariants(oldVariants);
        }
        else
        {
            productVariantList = VariantHelper.GetAllPossibleVariants(ProductID, NewCategories.Concat(ExistingCategories));
        }

        DataSet   ds = new DataSet("VariantsDS");
        DataTable dt = new DataTable("VariantsDT");

        // Combine new and existing categories in right order
        var combinedCategories = ExistingCategories.Union(NewCategories).OrderBy(cID => mAllCategoriesOptions.FindIndex(ca => ca.Item1.CategoryID == cID));

        // Build DataTable and UniGrid structure
        foreach (int categoryId in combinedCategories)
        {
            var optionCategory = mAllCategoriesOptions.FirstOrDefault(k => k.Item1.CategoryID == categoryId);

            // Add columns to DataTable
            dt.Columns.Add(new DataColumn(categoryId.ToString(), typeof(String)));

            // Add column with option category live site display name to the grid with variant preview. In case live site display name is not available option category display name is used
            if (optionCategory != null)
            {
                AddGridColumn(ResHelper.LocalizeString(optionCategory.Item1.CategoryTitle), categoryId.ToString(), "#transform: ecommerce.skuoption.SKUName");
            }
        }

        // Add RowNumber column to DataTable
        dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
        AddGridColumn("RowNumber", "RowNumber", string.Empty, true);

        // Add Exist column to DataTable
        dt.Columns.Add(new DataColumn("Exist", typeof(bool)));

        // Add Variant number column to DataTable and UniGrid
        dt.Columns.Add(new DataColumn("VariantNumber", typeof(string)));
        AddGridColumn(GetString("com.sku.skunumber"), "VariantNumber");

        // Fill DataTable
        int index = 0;

        foreach (ProductVariant productVariant in productVariantList)
        {
            DataRow dr = dt.NewRow();

            int i = 0;
            foreach (int categoryId in productVariant.ProductAttributes.CategoryIDs)
            {
                // Fill values to dynamically added column
                var optionCategory = mAllCategoriesOptions.FirstOrDefault(k => k.Item1.CategoryID == categoryId);

                if (optionCategory != null)
                {
                    dr[optionCategory.Item1.CategoryID.ToString()] = productVariant.ProductAttributes[i].SKUID;
                    i++;
                }
            }

            dr["RowNumber"]     = index;
            dr["Exist"]         = productVariant.Existing;
            dr["VariantNumber"] = productVariant.Variant.SKUNumber;

            dt.Rows.Add(dr);
            index++;
        }

        ds.Tables.Add(dt);
        return(ds);
    }
    /// <summary>
    /// Generates variants asynchronous.
    /// </summary>
    /// <param name="parameter">AsyncControl parameters</param>
    private void GenerateAndSave(object parameter)
    {
        try
        {
            // Regenerate already existing variants
            if (mRegenerateVariants)
            {
                ProductAttributeSet productAttributeSet = new ProductAttributeSet(CategorySelector.SelectedCategories.Values.Where(x => x > VariantOptionInfo.NewOption));
                List<ProductVariant> existingVariants = VariantHelper.AddNewCategoriesToVariantsOfProduct(ProductID, productAttributeSet);

                // Use special action contexts to turn off unnecessary actions
                using (ECommerceActionContext eCommerceContext = new ECommerceActionContext())
                {
                    eCommerceContext.TouchParent = false;
                    eCommerceContext.SetLowestPriceToParent = false;
                    eCommerceContext.AssignDefaultTaxesToProducts = false;

                    existingVariants.ForEach(pVariant =>
                        {
                            AddLog(HTMLHelper.HTMLEncode(ResHelper.LocalizeString(pVariant.Variant.SKUName)) + GetString("com.variants.isredefined"));
                            VariantHelper.SetProductVariant(pVariant);
                        });
                }

                // Save variant to update parent SKULastModified a SKUPrice properties
                var lastVariant = existingVariants.LastOrDefault();
                if (lastVariant != null)
                {
                    lastVariant.Variant.Generalized.SetObject();
                }
            }

            // Generate non-existing variants
            ProductVariant productVariant = null;

            // Use special action contexts to turn off unnecessary actions
            using (ECommerceActionContext eCommerceContext = new ECommerceActionContext())
            {
                eCommerceContext.TouchParent = false;
                eCommerceContext.SetLowestPriceToParent = false;
                eCommerceContext.AssignDefaultTaxesToProducts = false;

                foreach (DataRow row in mVariantsToGenerate)
                {
                    IEnumerable<int> options = GetAllOptions(row, NewCategories.Union(ExistingCategories));
                    productVariant = new ProductVariant(ProductID, new ProductAttributeSet(options));
                    AddLog(HTMLHelper.HTMLEncode(ResHelper.LocalizeString(productVariant.Variant.SKUName)) + GetString("com.variants.isdefined"));
                    productVariant.Set();
                }
            }

            // Save variant to update parent SKULastModified a SKUPrice properties
            if (productVariant != null)
            {
                productVariant.Variant.Generalized.SetObject();
            }

        }
        catch (Exception ex)
        {
            CurrentError = GetString("com.variant.definerror");
            EventLogProvider.LogException("Variant definition", "DEFINEVARIANT", ex);
        }
    }