Skip to content

Instantly share code, notes, and snippets.

@bomber-lomber
Created August 12, 2019 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bomber-lomber/df41b83ae1694a7d92f03ea4e7e8a442 to your computer and use it in GitHub Desktop.
Save bomber-lomber/df41b83ae1694a7d92f03ea4e7e8a442 to your computer and use it in GitHub Desktop.
DW recreate custom list field options
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