Developer forum

Forum » Development » Recreate Product custom field options

Recreate Product custom field options

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

I have a custom development where I need to recreate the field options of a list box from a product custom field. 

 

I am successfully deleting all existing options and clearing the cache, but I cannot find the right method to do it.

 

How can I achieve this?

 

Best Regards,

Nuno Aguiar


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Nuno,

You may be missing the step to create a language version of the option, ie. a translation. Once you've created the FieldOption and bound it to the field using the ID, you also need a FieldOptionTranslation that binds the name of the option to a specific language.

- Jeppe

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Jeppe,

 

Well, I am still trying to create the FieldOption (through the API). I know I have to create a language version of it, and I would assume the LanguageID to be one of the parameters.

 

Could it be the method is private, and that's why I can't find it?

 

Nuno

 
Alexander Gubenko
Alexander Gubenko
Reply
This post has been marked as an answer

Hi Nuno,

I've wrote small example which allow you recreate custom field lists 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();
}

The full class is here: gist

Votes for this answer: 1
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

HI Alexander,

 

Thanks. This was helpful and I was able to get it working as I needed from this code.

 

FYI: This was built as an ItemNotification, so that when someone added/deleted/updated an item, we'd recreate field options. Then in the product detail template we'd instantiate the Item to get the necessary data.

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum