-
-
Save bomber-lomber/df41b83ae1694a7d92f03ea4e7e8a442 to your computer and use it in GitHub Desktop.
DW recreate custom list field options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Dynamicweb.Controls.Extensibility; | |
using Dynamicweb.Core.UI.Icons; | |
using Dynamicweb.Ecommerce.Products; | |
using Dynamicweb.Extensibility.AddIns; | |
using System; | |
namespace Dynamicweb.Controls.Examples | |
{ | |
[AddInTarget("/Admin/Module/eCom_Catalog/dw7/PIM/PimProductList.aspx")] | |
public class CustomFieldListOptions : RibbonBarAddIn | |
{ | |
private RibbonBar ribbon; | |
public CustomFieldListOptions(RibbonBar ribbon) : base(ribbon) | |
{ | |
this.ribbon = ribbon; | |
} | |
public override void Load() | |
{ | |
RibbonBarGroup ribbonBarGroup = base.CreateDefaultContainer(); | |
ribbonBarGroup.Name = "CustomFields"; | |
var recreateListWithOptionsSet1Button = new RibbonBarButton(); | |
recreateListWithOptionsSet1Button.Click += RecreateListWithOptionsSet1Button_Click; | |
recreateListWithOptionsSet1Button.EnableServerClick = true; | |
recreateListWithOptionsSet1Button.Text = "Options set 1"; | |
recreateListWithOptionsSet1Button.Icon = KnownIcon.AddCircle; | |
recreateListWithOptionsSet1Button.Size = Dynamicweb.Controls.Icons.Icon.Size.Medium; | |
ribbonBarGroup.AddItem(recreateListWithOptionsSet1Button); | |
var recreateListWithOptionsSet2Button = new RibbonBarButton(); | |
recreateListWithOptionsSet2Button.Click += RecreateListWithOptionsSet2Button_Click; | |
recreateListWithOptionsSet2Button.EnableServerClick = true; | |
recreateListWithOptionsSet2Button.Text = "Options set 2"; | |
recreateListWithOptionsSet2Button.Icon = KnownIcon.AddToPhotos; | |
recreateListWithOptionsSet2Button.Size = Dynamicweb.Controls.Icons.Icon.Size.Medium; | |
ribbonBarGroup.AddItem(recreateListWithOptionsSet2Button); | |
} | |
protected void RecreateListWithOptionsSet2Button_Click(object sender, EventArgs e) | |
{ | |
var ListFieldId = "FIELD13"; | |
var options = new FieldOptionCollection(); | |
options.Add(new FieldOption() { Name = "x1", Value = "x1" }); | |
options.Add(new FieldOption() { Name = "x2", Value = "x2" }); | |
options.Add(new FieldOption() { Name = "x3", Value = "x3" }); | |
RecreateCustomListFieldOptions(ListFieldId, options); | |
} | |
protected void RecreateListWithOptionsSet1Button_Click(object sender, EventArgs e) | |
{ | |
var ListFieldId = "FIELD13"; | |
var options = new FieldOptionCollection(); | |
options.Add(new FieldOption() { Name = "y1", Value = "y1" }); | |
options.Add(new FieldOption() { Name = "y2", Value = "y2" }); | |
RecreateCustomListFieldOptions(ListFieldId, options); | |
} | |
private void RecreateCustomListFieldOptions(string fieldId, FieldOptionCollection options) | |
{ | |
var field = new ProductField(fieldId); | |
Ecommerce.Common.Application.KillProductFields(); | |
FieldOption.DeleteAll(fieldId); | |
foreach (var opt in options) | |
{ | |
opt.FieldId = fieldId; | |
opt.Save(); | |
} | |
FieldOptionTranslation.DeleteExcessOptions(); | |
foreach (var language in Ecommerce.Services.Languages.GetLanguages()) | |
{ | |
foreach (var opt in options) | |
{ | |
var translatedOption = FieldOptionTranslation.GetTranslatedOption(opt.Id, language.LanguageId); | |
if (translatedOption == null) | |
{ | |
translatedOption = new FieldOptionTranslation() | |
{ | |
LanguageId = language.LanguageId, | |
OptionId = opt.Id | |
}; | |
} | |
translatedOption.Name = opt.Name; | |
translatedOption.Save(); | |
} | |
} | |
FieldOptionTranslation.ClearCache(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment