/// <summary> /// Returns an enumerable collection of all variants of the given product. /// </summary> /// <param name="productId">SKU object identifier of the variant's parent product.</param> /// <returns>Collection of product variants. See <see cref="ProductVariant"/> for detailed information.</returns> public IEnumerable <ProductVariant> GetByProductId(int productId) { var variantSKUs = VariantHelper.GetVariants(productId).OnSite(SiteContext.CurrentSiteID).ToList(); // Create variants with the options return(variantSKUs.Select(sku => new ProductVariant(sku.SKUID))); }
/// <summary> /// Handles the OnReloadData event of the menuZoneVariants control. /// </summary> protected void menuMoveToZoneVariants_OnReloadData(object sender, EventArgs e) { // Check permissions if (currentUser == null) { return; } if ((DocumentContext.CurrentPageInfo != null) && (DocumentContext.CurrentPageInfo.TemplateInstance != null)) { VariantModeEnum currentVariantMode = VariantModeEnum.None; string targetZoneId = ValidationHelper.GetString(menuMoveToZoneVariants.Parameter, string.Empty); int pageTemplateId = DocumentContext.CurrentPageInfo.UsedPageTemplateInfo.PageTemplateId; // Get selected zone variant mode if ((DocumentContext.CurrentPageInfo != null) && (DocumentContext.CurrentPageInfo.TemplateInstance != null)) { WebPartZoneInstance targetZone = DocumentContext.CurrentPageInfo.TemplateInstance.GetZone(targetZoneId); if (targetZone != null) { currentVariantMode = targetZone.VariantMode; } } SetColumnNames(currentVariantMode); // Get all zone variants of the current web part DataTable resultTable = null; DataSet ds = VariantHelper.GetVariants(currentVariantMode, pageTemplateId, targetZoneId, Guid.Empty, 0); if (!DataHelper.DataSourceIsEmpty(ds)) { DataTable table = ds.Tables[0].Copy(); table.DefaultView.Sort = columnVariantID; // Add the original web part as the first item in the variant list DataRow originalVariant = table.NewRow(); originalVariant[columnVariantID] = 0; originalVariant[columnVariantDisplayName] = ResHelper.GetString("ZoneMenu.OriginalZone", UICulture); originalVariant[columnVariantZoneID] = targetZoneId; originalVariant[columnVariantPageTemplateID] = pageTemplateId; originalVariant[columnVariantInstanceGUID] = Guid.Empty; table.Rows.InsertAt(originalVariant, 0); resultTable = table.DefaultView.ToTable(); if (DataHelper.DataSourceIsEmpty(resultTable)) { pnlNoZoneVariants.Visible = true; ltlNoZoneVariants.Text = ResHelper.GetString("Content.NoPermissions", UICulture); } } repMoveToZoneVariants.DataSource = resultTable; repMoveToZoneVariants.DataBind(); } }
/// <summary> /// Updates price of variants in asynchronous control. /// </summary> /// <param name="newPrice">New price of variant</param> protected void UpdatePrice(double newPrice) { try { SKUInfo variantInfo = null; // Use special action contexts to turn off unnecessary actions using (ECommerceActionContext eCommerceContext = new ECommerceActionContext()) { eCommerceContext.TouchParent = false; eCommerceContext.SetLowestPriceToParent = false; // Update all variants if ((WhatEnum)ValidationHelper.GetInteger(drpWhat.SelectedIndex, -1) == WhatEnum.AllVariants) { var variants = VariantHelper.GetVariants(ProductID); foreach (var variant in variants) { variantInfo = variant; UpdateVariant(variantInfo, newPrice); } } // Update selected variants else { var variantsToUpdate = ugVariants.SelectedItems; foreach (var variantId in variantsToUpdate) { variantInfo = SKUInfoProvider.GetSKUInfo(ValidationHelper.GetInteger(variantId, 0)); // Do not allow modify variants of other product if ((variantInfo != null) && (variantInfo.SKUParentSKUID != ProductID)) { variantInfo = null; } if (variantInfo != null) { UpdateVariant(variantInfo, newPrice); } } } } // Save variant to update parent SKULastModified a SKUPrice properties if (variantInfo != null) { variantInfo.Generalized.SetObject(); } } catch (Exception ex) { CurrentError = GetString("com.product.updatepriceerror"); EventLogProvider.LogException("Update price bulk action", "UPDATEVARIANT", ex); } }
/// <summary> /// Handles the OnReloadData event of the menuWebPartCPVariants control. /// </summary> protected void menuWebPartCPVariants_OnReloadData(object sender, EventArgs e) { // Check permissions if ((MembershipContext.AuthenticatedUser == null) || (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("CMS.ContentPersonalization", "Read"))) { return; } string parameters = ValidationHelper.GetString(menuWebPartCPVariants.Parameter, string.Empty); string[] items = parameters.Split(new char[] { ',' }, 7); if ((items == null) || (items.Length < 4)) { return; } string zoneId = ValidationHelper.GetString(items[0], string.Empty); string webpartName = ValidationHelper.GetString(items[1], string.Empty); string aliasPath = ValidationHelper.GetString(items[2], string.Empty); Guid instanceGuid = ValidationHelper.GetGuid(items[3], Guid.Empty); isZone = (instanceGuid == Guid.Empty); if ((DocumentContext.CurrentPageInfo != null) && (DocumentContext.CurrentPageInfo.TemplateInstance != null)) { int templateId = DocumentContext.CurrentPageInfo.UsedPageTemplateInfo.PageTemplateId; DataSet ds = VariantHelper.GetVariants(VariantModeEnum.ContentPersonalization, templateId, zoneId, instanceGuid, 0); DataTable resultTable = null; if (!DataHelper.DataSourceIsEmpty(ds)) { DataTable table = ds.Tables[0].Copy(); table.DefaultView.Sort = columnVariantID; // Add the original web part as the first item in the variant list DataRow originalVariant = table.NewRow(); originalVariant[columnVariantID] = 0; originalVariant[columnVariantDisplayName] = ResHelper.GetString(isZone ? "ZoneMenu.OriginalZone" : "WebPartMenu.OriginalWebPart"); originalVariant[columnVariantZoneID] = zoneId; originalVariant[columnVariantPageTemplateID] = templateId; originalVariant[columnVariantInstanceGUID] = instanceGuid; table.Rows.InsertAt(originalVariant, 0); resultTable = table.DefaultView.ToTable(); } repWebPartCPVariants.DataSource = resultTable; repWebPartCPVariants.DataBind(); } }
/// <summary> /// Handles the OnReloadData event of the menuWebPartVariants control. /// </summary> protected void menuZoneMVTVariants_OnReloadData(object sender, EventArgs e) { // Check permissions if ((currentUser == null) || (!currentUser.IsAuthorizedPerResource("CMS.MVTest", "Read"))) { return; } SetColumnNames(VariantModeEnum.MVT); string zoneId = ValidationHelper.GetString(menuZoneMVTVariants.Parameter, string.Empty); if ((DocumentContext.CurrentPageInfo != null) && (DocumentContext.CurrentPageInfo.TemplateInstance != null)) { int templateId = DocumentContext.CurrentPageInfo.UsedPageTemplateInfo.PageTemplateId; // Get all MVT zone variants of the current web part DataSet ds = VariantHelper.GetVariants(VariantModeEnum.MVT, templateId, zoneId, Guid.Empty, 0); DataTable resultTable = null; if (!DataHelper.DataSourceIsEmpty(ds)) { DataTable table = ds.Tables[0].Copy(); table.DefaultView.Sort = columnVariantID; // Add the original web part as the first item in the variant list DataRow originalVariant = table.NewRow(); originalVariant[columnVariantID] = 0; originalVariant[columnVariantDisplayName] = ResHelper.GetString("WebPartMenu.OriginalWebPart", UICulture); originalVariant[columnVariantZoneID] = zoneId; originalVariant[columnVariantPageTemplateID] = templateId; originalVariant[columnVariantInstanceGUID] = Guid.Empty; table.Rows.InsertAt(originalVariant, 0); resultTable = table.DefaultView.ToTable(); if (DataHelper.DataSourceIsEmpty(resultTable)) { pnlNoZoneMVTVariants.Visible = true; lblNoZoneMVTVariants.Text = ResHelper.GetString("Content.NoPermissions", UICulture); } } else { pnlNoZoneMVTVariants.Visible = true; } repZoneMVTVariants.DataSource = resultTable; repZoneMVTVariants.DataBind(); } }
/// <summary> /// Gets all options used in variant. /// </summary> /// <returns>Collection of option Ids used in variants.</returns> private List <string> GetOptionIdsUsedInVariant() { var optionsInCategory = SKUInfoProvider.GetSKUOptions(optionSKUCategoryInfo.CategoryID, false).Column("SKUID"); var optionsInVariants = VariantOptionInfo.Provider.Get() .WhereIn("VariantSKUID", VariantHelper.GetVariants(ProductID).Column("SKUID")) .Column("OptionSKUID"); // Check if some variant was created from the option return(optionsInCategory.WhereIn("SKUID", optionsInVariants).ToList() .Select(o => o.SKUID.ToString()) .ToList()); }
private IEnumerable <SKUInfo> GetOptions(int skuID, int categoryID, ISKUInfoProvider skuInfoProvider) { // Get all variant's options var variantOptionIDs = VariantOptionInfo.Provider.Get() .WhereIn("VariantSKUID", VariantHelper.GetVariants(skuID).Column("SKUID")) .Column("OptionSKUID"); var variantOptionsList = skuInfoProvider.Get() .WhereIn("SKUID", variantOptionIDs) .OrderBy("SKUOrder") .ToList(); // Create option categories with selectable variant options return(variantOptionsList.Where(o => o.SKUOptionCategoryID == categoryID)); }
//EndDocSection:DisplayProduct //DocSection:DisplayVariant /// <summary> /// Displays product detail page of a product or product variant specified by the GUID of the product's or variant's page. /// </summary> /// <param name="guid">Node GUID of the product's (variant's) page.</param> /// <param name="productAlias">Node alias of the product's (variant's) page.</param> public ActionResult Detail(Guid guid, string productAlias) { // Gets the product from the connected Kentico database SKUTreeNode product = GetProduct(guid); // If the product is not found or if it is not allowed for sale, redirects to error 404 if ((product == null) || !product.SKU.SKUEnabled) { return(HttpNotFound()); } // Redirects if the specified page alias does not match if (!string.IsNullOrEmpty(productAlias) && !product.NodeAlias.Equals(productAlias, StringComparison.InvariantCultureIgnoreCase)) { return(RedirectToActionPermanent("Detail", new { guid = product.NodeGUID, productAlias = product.NodeAlias })); } // Gets all product variants of the product List <ProductVariant> variants = VariantHelper.GetVariants(product.NodeSKUID).OnSite(SiteContext.CurrentSiteID).ToList() .Select(sku => new ProductVariant(sku.SKUID)) .OrderBy(v => v.Variant.SKUPrice).ToList(); // Selects the first product variant ProductVariant selectedVariant = variants.FirstOrDefault(); // Calculates the price of the product or the variant ShoppingCartInfo cart = shoppingService.GetCurrentShoppingCart(); SKUInfo selectedProduct = selectedVariant != null ? selectedVariant.Variant : product.SKU; ProductCatalogPrices priceDetail = calculatorFactory .GetCalculator(cart.ShoppingCartSiteID) .GetPrices(selectedProduct, Enumerable.Empty <SKUInfo>(), cart); // Initializes the view model of the product or product variant ProductViewModel viewModel = new ProductViewModel(product, priceDetail, variants, selectedVariant?.Variant?.SKUID ?? 0); // Displays the product detail page return(View(viewModel)); }
/// <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> /// Returns an enumerable collection of all variants of the given product. /// </summary> /// <param name="productId">SKU object identifier of the variant's parent product.</param> /// <returns>Collection of product variants. See <see cref="Variant"/> for detailed information.</returns> public IEnumerable <Variant> GetByProductId(int productId) { var variantSKUs = VariantHelper.GetVariants(productId).OnSite(SiteID).ToList(); // Get the used option IDs for the variants var variantOptions = VariantOptionInfoProvider.GetVariantOptions() .WhereIn("VariantSKUID", variantSKUs.Select(s => s.SKUID).ToList()) .ToList(); // Pre-load option SKUs for the variants var options = SKUInfoProvider.GetSKUs() .WhereIn("SKUID", variantOptions.Select(v => v.OptionSKUID).ToList()) .ToList(); // Create variants with the options return(variantSKUs.Select(sku => new Variant( sku, new ProductAttributeSet(variantOptions .Where(o => o.VariantSKUID == sku.SKUID) .Select(o => options.First(s => s.SKUID == o.OptionSKUID)) .ToArray()) ))); }
//EndDocSection:DisplayProduct //DocSection:DisplayVariant /// <summary> /// Displays product detail page of a product or product variant. /// </summary> public ActionResult Detail() { // Gets the product from the connected Xperience database SKUTreeNode product = GetProduct(); // If the product is not found or if it is not allowed for sale, redirects to error 404 if ((product == null) || !product.SKU.SKUEnabled) { return(NotFound()); } // Gets all product variants of the product List <ProductVariant> variants = VariantHelper .GetVariants(product.NodeSKUID) .OnSite(siteService.CurrentSite.SiteID).ToList() .Select(sku => new ProductVariant(sku.SKUID)) .OrderBy(v => v.Variant.SKUPrice).ToList(); // Selects the first product variant ProductVariant selectedVariant = variants.FirstOrDefault(); // Calculates the price of the product or the variant ShoppingCartInfo cart = shoppingService.GetCurrentShoppingCart(); SKUInfo selectedProduct = selectedVariant != null ? selectedVariant.Variant : product.SKU; ProductCatalogPrices priceDetail = priceCalculatorFactory .GetCalculator(cart.ShoppingCartSiteID) .GetPrices(selectedProduct, Enumerable.Empty <SKUInfo>(), cart); // Initializes the view model of the product or product variant ProductViewModel viewModel = new ProductViewModel(product, priceDetail, variants, selectedVariant?.Variant?.SKUID ?? 0); // Displays the product detail page return(View(viewModel)); }
//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 }
/// <summary> /// Deletes variants is asynchronous control. /// </summary> private void Delete(object parameters) { bool variantWasDisabled = false; try { // Use special action contexts to turn off unnecessary actions using (ECommerceActionContext eCommerceContext = new ECommerceActionContext()) { eCommerceContext.TouchParent = false; eCommerceContext.SetLowestPriceToParent = false; // Delete all variants if ((WhatEnum)ValidationHelper.GetInteger(drpWhat.SelectedIndex, -1) == WhatEnum.AllVariants) { var variants = VariantHelper.GetVariants(ProductID); foreach (SKUInfo variant in variants) { // Set flag when variant was disabled due to dependencies variantWasDisabled |= !DeleteVariant(variant); } } // Delete selected variants else { var variantsToDelete = ugVariants.SelectedItems; foreach (string variantId in variantsToDelete) { var variantInfo = SKUInfoProvider.GetSKUInfo(ValidationHelper.GetInteger(variantId, 0)); if ((variantInfo != null) && (variantInfo.SKUParentSKUID == ProductID)) { // Set flag when variant was disabled due to dependencies variantWasDisabled |= !DeleteVariant(variantInfo); } } } } // Save variant to update parent SKULastModified a SKUPrice properties DataSet productVariants = VariantHelper.GetVariants(ProductID); if (!DataHelper.DataSourceIsEmpty(productVariants)) { SKUInfo variantInfo = new SKUInfo(productVariants.Tables[0].Rows[0]); variantInfo.Generalized.SetObject(); } } catch (Exception ex) { CurrentError = GetString("com.product.deleteerror"); EventLogProvider.LogException("Variant listing", "DELETEVARIANT", ex); } ugVariants.ResetSelection(); if (variantWasDisabled) { CurrentWarning = GetString("com.product.edit.disablevariant"); } }