/// <summary> /// Overrides OnPreInit method. /// </summary> protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); // Initialize creation of a new variants GlobalObjectsKeyName = ECommerceSettings.ALLOW_GLOBAL_PRODUCTS; // Check if product belongs to current site var product = EditedObject as SKUInfo; if (product != null) { CheckEditedObjectSiteID(product.SKUSiteID); } mAllCategoriesOptions = new List <Tuple <OptionCategoryInfo, List <SKUInfo> > >(); mVariantCategoriesOptions = new List <Tuple <OptionCategoryInfo, List <SKUInfo> > >(); // Get all enabled product option attribute categories plus option categories used in variants DataSet allCategoriesDS = VariantHelper.GetUsedProductOptionCategories(ProductID, OptionCategoryTypeEnum.Attribute); // Get all product options categories which are already in variants DataSet variantCategoriesDS = VariantHelper.GetProductVariantsCategories(ProductID); // Fill categories lists FillCategoriesOptions(mAllCategoriesOptions, allCategoriesDS); FillCategoriesOptions(mVariantCategoriesOptions, variantCategoriesDS); // Pass data to controls VariantFilter.ExternalDataSource = CategorySelector.AllCategoriesOptions = mAllCategoriesOptions.ToDictionary(c => c.Item1, c => c.Item2);; CategorySelector.VariantCategoriesOptions = mVariantCategoriesOptions.ToDictionary(c => c.Item1, c => c.Item2);; }
/// <summary> /// Returns a collection of option categories used in a product's variants. /// </summary> /// <param name="productId">SKU identifier of the variant's parent product.</param> /// <returns>Collection of option categories used in a product's variants. See <see cref="OptionCategoryInfo"/> for detailed information.</returns> public IEnumerable <OptionCategoryInfo> GetVariantOptionCategories(int productId) { return(repositoryCacheHelper.CacheObjects(() => { // Get a list of option categories return VariantHelper.GetProductVariantsCategories(productId); }, $"{nameof(VariantRepository)}|{nameof(GetVariantOptionCategories)}|{productId}")); }
/// <summary> /// Gets the categories with names from external data source or database. /// </summary> /// <param name="skuIdOfProductWithCategories">Id of product with categories.</param> private Dictionary <int, string> GetCategoriesWithNames(int skuIdOfProductWithCategories) { Dictionary <int, string> categoriesWithName = new Dictionary <int, string>(); if ((ExternalDataSource != null) && (ExternalDataSource.Count > 0)) { // External data foreach (OptionCategoryInfo info in ExternalDataSource.Keys) { // Use category live site display name in case it is available, otherwise category display name categoriesWithName.Add(info.CategoryID, info.CategoryTitle); } return(categoriesWithName); } // Load data from DB DataSet dsOptionCategories; // If ShowOnlyUsedCategories is true, dataset will contain only already used categories in variants if (ShowOnlyUsedCategories) { dsOptionCategories = VariantHelper.GetProductVariantsCategories(skuIdOfProductWithCategories); } else { dsOptionCategories = OptionCategoryInfoProvider.GetProductOptionCategories(skuIdOfProductWithCategories, true, OptionCategoryTypeEnum.Attribute); } if (DataHelper.DataSourceIsEmpty(dsOptionCategories)) { return(categoriesWithName); } foreach (DataRow catDr in dsOptionCategories.Tables[0].Rows) { int categoryId = ValidationHelper.GetInteger(catDr["CategoryID"], 0); string categoryName = ValidationHelper.GetString(catDr["CategoryLiveSiteDisplayName"], String.Empty); if (string.IsNullOrEmpty(categoryName)) { categoryName = ValidationHelper.GetString(catDr["CategoryDisplayName"], String.Empty); } categoriesWithName.Add(categoryId, categoryName); } return(categoriesWithName); }
/// <summary> /// Creates category with variant attributes. /// </summary> /// <param name="optionsList">Product options</param> private void SetVariantAttributes(List <SKUInfo> optionsList) { // Get attributes category index - just before representing category var attrPos = editForm.FormInformation.ItemsList.FindIndex(f => (f is FormCategoryInfo) && ((FormCategoryInfo)f).CategoryName.EqualsCSafe("com.sku.representingcategory")); // Create attributes category var attCategory = new FormCategoryInfo() { CategoryName = "Attributes", IsDummy = true, }; attCategory.SetPropertyValue(FormCategoryPropertyEnum.Caption, HTMLHelper.HTMLEncode(GetString("com.variant.attributes"))); editForm.FormInformation.AddFormItem(attCategory, attrPos++); // Get variant categories var variantCategories = VariantHelper.GetProductVariantsCategories(ProductID, false); foreach (var category in variantCategories) { var option = optionsList.FirstOrDefault(o => o.SKUOptionCategoryID == category.CategoryID); if (option != null) { FormFieldInfo ffOption = new FormFieldInfo { Name = category.CategoryName, AllowEmpty = true, Size = 400, FieldType = FormFieldControlTypeEnum.LabelControl, DataType = FieldDataType.Text, IsDummyField = true, }; ffOption.SetPropertyValue(FormFieldPropertyEnum.DefaultValue, HTMLHelper.HTMLEncode(ResHelper.LocalizeString(option.SKUName))); // Show category live site display name instead of category display name in case it is available ffOption.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, HTMLHelper.HTMLEncode(ResHelper.LocalizeString(category.CategoryTitle))); //Insert field to the form on specified position editForm.FormInformation.AddFormItem(ffOption, attrPos++); } } }
/// <summary> /// Returns a collection of option categories used in a product's variants. /// </summary> /// <param name="productId">SKU identifier of the variant's parent product.</param> /// <returns>Collection of option categories used in a product's variants. See <see cref="ProductOptionCategory"/> for detailed information.</returns> public IEnumerable <ProductOptionCategory> GetVariantOptionCategories(int productId) { // Get a list of option categories var optionCategoriesList = VariantHelper.GetProductVariantsCategories(productId).ToList(); // Get all variant's options var variantOptionIDs = VariantOptionInfoProvider.GetVariantOptions() .WhereIn("VariantSKUID", VariantHelper.GetVariants(productId).Column("SKUID")) .Column("OptionSKUID"); var variantOptionsList = SKUInfoProvider.GetSKUs() .WhereIn("SKUID", variantOptionIDs) .OrderBy("SKUOrder") .ToList(); // Create option categories with selectable variant options return(optionCategoriesList.Select(cat => new ProductOptionCategory( cat, variantOptionsList.Where(o => o.SKUOptionCategoryID == cat.CategoryID) ) )); }
/// <summary> /// Removes category bindings assigned to SKU. /// </summary> /// <param name="categoryIDs">ID of categories which should be removed</param> /// <returns>Returns true if some category binding was removed.</returns> private bool RemoveSKUCategoryBindings(IEnumerable <int> categoryIDs) { var ids = categoryIDs.ToList(); // GetProductVariantsCategories create a complex query with join included -> column must be defined with table name prefix // Convert query to list before calling Select, otherwise there will be ambiguous column CategoryID var variantCategoryIDs = VariantHelper.GetProductVariantsCategories(ProductID, false).Column("COM_OptionCategory.CategoryID").ToList().Select(category => category.CategoryID).ToList(); var categoryIDsToRemove = ids.Except(variantCategoryIDs).ToList(); foreach (var id in categoryIDsToRemove) { ProductHelper.RemoveOptionCategory(ProductID, id); } // Categories are already used in variants -> they cannot be removed if (ids.Except(categoryIDsToRemove).Any()) { ShowWarning(GetString("com.optioncategory.remove")); } return(categoryIDsToRemove.Any()); }
/// <summary> /// Loads control. /// </summary> private void LoadControl() { // Get all product categories with options which are already in variants if (VariantCategoriesOptions.Count == 0) { DataSet variantCategoriesDS = VariantHelper.GetProductVariantsCategories(ProductID); FillCategoriesOptionsDictionary(VariantCategoriesOptions, variantCategoriesDS); } // Get all product attribute categories with options if (AllCategoriesOptions.Count == 0) { DataSet allCategoriesDS = OptionCategoryInfoProvider.GetProductOptionCategories(ProductID, true, OptionCategoryTypeEnum.Attribute); FillCategoriesOptionsDictionary(AllCategoriesOptions, allCategoriesDS); } foreach (KeyValuePair <OptionCategoryInfo, List <SKUInfo> > keyValuePair in AllCategoriesOptions) { if (keyValuePair.Value.Count > 0) { OptionCategoryInfo optionCategory = keyValuePair.Key; // Create new instance of CheckBoxWithDropDown control and prefill all necessary values CheckBoxWithDropDown checkBoxWithDropDown = new CheckBoxWithDropDown(); checkBoxWithDropDown.ID = ValidationHelper.GetString(optionCategory.CategoryID, string.Empty); checkBoxWithDropDown.Value = optionCategory.CategoryID; // Use live site display name instead of category display name in case it is available checkBoxWithDropDown.CheckboxText = HTMLHelper.HTMLEncode(ResHelper.LocalizeString(optionCategory.CategoryTitle)); // Attach listeners checkBoxWithDropDown.OnCheckBoxSelectionChanged += checkBoxWithDropDown_OnCheckBoxSelectionChanged; checkBoxWithDropDown.OnDropDownSelectionChanged += checkBoxWithDropDown_OnDropDownSelectionChanged; // Option category is in variants too if (VariantCategoriesOptions.Keys.Any(c => ValidationHelper.GetInteger(c["categoryId"], 0) == optionCategory.CategoryID)) { // Check and disable checkbox checkBoxWithDropDown.CheckboxChecked = true; checkBoxWithDropDown.Enabled = false; // Already existing variants add to selected categories too if (!SelectedCategories.ContainsKey(optionCategory.CategoryID)) { SelectedCategories.Add(optionCategory.CategoryID, VariantOptionInfo.ExistingSelectedOption); } } // Option category is not in variant, but some categories in variants already exists else if (VariantCategoriesOptions.Count > 0) { // Set prompt message and visibility checkBoxWithDropDown.DropDownPrompt = GetString("general.pleaseselect"); checkBoxWithDropDown.DropDownLabel = GetString("com.variants.dropdownlabel"); checkBoxWithDropDown.DropDownVisible = true; // Get all product options and bind them to dropdownlist var options = SKUInfoProvider.GetSKUOptionsForProduct(ProductID, optionCategory.CategoryID, true).OrderBy("SKUOrder"); var dropDownItems = new ListItemCollection(); foreach (var option in options) { dropDownItems.Add(new ListItem(option.SKUName, option.SKUID.ToString())); } checkBoxWithDropDown.DropDownItems = dropDownItems; } // Finally bind this control to parent chboxPanel.Controls.Add(checkBoxWithDropDown); } } }
//EndDocSection:DifferentShippingAddress private object DummyEcommerceMethod() { ShoppingCartInfo shoppingCart = null; SKUInfo productSku = null; ProductVariant variant = null; SKUTreeNode product = null; SKUInfo sku = null; DummyViewModel model = null; OrderInfo order = null; PaymentResultInfo result = null; //DocSection:CalculatePriceOptions ProductCatalogPrices productPrice = Service.Resolve <ICatalogPriceCalculatorFactory>() .GetCalculator(shoppingCart.ShoppingCartSiteID) .GetPrices(productSku, Enumerable.Empty <SKUInfo>(), shoppingCart); //EndDocSection:CalculatePriceOptions //DocSection:FormatPriceOptions decimal price = 5.50M; string formattedPrice = String.Format(shoppingCart.Currency.CurrencyFormatString, price); //EndDocSection:FormatPriceOptions //DocSection:VariantDisplayImg var response = new { // ... imagePath = Url.Content(variant.Variant.SKUImagePath) }; //EndDocSection:VariantDisplayImg //DocSection:DisplayAttributeSelection // Gets the cheapest variant of the product List <ProductVariant> variants = VariantHelper.GetVariants(product.NodeSKUID).OnSite(SiteContext.CurrentSiteID).ToList() .Select(s => new ProductVariant(s.SKUID)) .OrderBy(v => v.Variant.SKUPrice).ToList(); ProductVariant cheapestVariant = variants.FirstOrDefault(); // Gets the product's option categories IEnumerable <OptionCategoryInfo> categories = VariantHelper.GetProductVariantsCategories(sku.SKUID).ToList(); // Gets the cheapest variant's selected attributes IEnumerable <ProductOptionCategoryViewModel> variantCategories = cheapestVariant?.ProductAttributes.Select( option => new ProductOptionCategoryViewModel(sku.SKUID, option.SKUID, categories.FirstOrDefault(c => c.CategoryID == option.SKUOptionCategoryID))); //EndDocSection:DisplayAttributeSelection //DocSection:ShippingIsDifferent if (model.IsShippingAddressDifferent) { // ... } //EndDocSection:ShippingIsDifferent //DocSection:DifferentPaymentMethods if (shoppingCart.PaymentOption.PaymentOptionName.Equals("PaymentMethodCodeName")) { return(RedirectToAction("ActionForPayment", "MyPaymentGateway")); } //EndDocSection:DifferentPaymentMethods //DocSection:SetPaymentResult order.UpdateOrderStatus(result); //EndDocSection:SetPaymentResult //DocSection:RedirectForManualPayment return(RedirectToAction("ThankYou", new { orderID = order.OrderID })); //EndDocSection:RedirectForManualPayment }
public IEnumerable <OptionCategoryInfo> GetVariantOptionCategories(int productId) { // Get a list of option categories return(VariantHelper.GetProductVariantsCategories(productId).ToList()); }