Hi there,
How do I add options to a Product Category Field (of type Field) programmtically? I tried things like this:
private static void SaveFieldOptions(string fieldId, Dictionary<string, string> options)
{
var defaultLanguageId = Dynamicweb.Ecommerce.Services.Languages.GetDefaultLanguageId();
var optionList = new List<FieldOption>();
foreach (var kvp in options)
{
var fieldOption = new FieldOption
{
Id = kvp.Key,
Value = kvp.Value,
FieldId = fieldId,
};
optionList.Add(fieldOption);
var fieldOptionTranslation = new FieldOptionTranslation(defaultLanguageId)
{
Name = kvp.Value
};
fieldOption.Translations.Add(fieldOptionTranslation);
fieldOption.SetName(defaultLanguageId, kvp.Value);
Dynamicweb.Ecommerce.Services.FieldOptions.Save(fieldOption);
Dynamicweb.Ecommerce.Services.FieldOptions.SaveTranslation(fieldOptionTranslation);
}
Dynamicweb.Ecommerce.Services.FieldOptions.SaveOptions(optionList);
}
That looks like far too many Saves to me and it still doesn't work.
I looked at Field.FieldOptions but that tells me it's obsolete and refers me to the FieldOptions service.
The Docs Bot tells me this :-)
-------------------
Dynamicweb’s documentation for ProductCategoryField and option translations is quite sparse, and there is no complete end‑to‑end example of creating options with translations purely in code.
From the available API docs we do have this key piece:
- There is a
Dynamicweb.Ecommerce.Products.FieldOptionTranslationclass, which represents a translation for a list option on a field (e.g. a list box option on a product field or category field).
However, the documentation does not show:
- How
FieldOptionTranslationis attached to a specific option on aProductCategoryField - Which collection/property on the option or field you should populate
- Any repository/service methods that persist these translations programmatically
Because of that, I cannot give you a correct, documented code sample such as:
—that would be speculative and not backed by the official docs.
-------------------
Any samle code would be appreciated.
Imar