Product sorting on TextOption Multiselect

How to sort products on a SystemFieldTypeConstants.TextOption value?

For example, in the SearchQueryBuilder would I like to change SearchQueryConstants.Popular to be sorting on the TextOption.Item with value “MestSalda”.

SearchQueryBuilder:

public void ApplyCategorySorting()
{
    switch (_searchQuery.SortBy)
    {
        ...

        case SearchQueryConstants.Popular:
            _request.Sortings.Add(new Sorting(FilteringConstants.GetMostSoldTagName(_requestModelAccessor.RequestModel.WebsiteModel.SystemId), SortDirection.Descending, SortingFieldType.Float));
            break;
        
        ...
    }
}

TextOption definition in ProductsFieldDefinitionSetup:

new FieldDefinition<ProductArea>(ProductFieldNameConstants.ItemExtraLabels, SystemFieldTypeConstants.TextOption)
{
    CanBeGridColumn = false,
    CanBeGridFilter = false,
    Option = new TextOption
    {
        MultiSelect = true,
        Items = new List<TextOption.Item>
        {
            new TextOption.Item
            {
                Value = "MestSalda",
                Name = new Dictionary<string, string> { { "en-US", "Most sold" }, { "sv-SE", "Mest sålda" } }
            },
            new TextOption.Item
            {
                Value = "Kampanj",
                Name = new Dictionary<string, string> { { "en-US", "Promotional" }, { "sv-SE", "Kampanj" } }
            },
            new TextOption.Item
            {
                Value = "BraMiljoval",
                Name = new Dictionary<string, string> { { "en-US", "Good Eco-choice" }, { "sv-SE", "Bra miljöval" } }
            },
        }
    }
},

Litium version: 7.2.3

You can get the field option value like this:

var field = _fieldDefinitionService.Get<ProductArea>(ProductFieldNameConstants.ItemExtraLabels);
var options = field.Option as TextOption;
var option = options?.Items.FirstOrDefault(x => x.Value == "MestSalda");

Thanks Steve, I guess that was a little bit useful, but my problem is that I don’t see how to convert the field option value to a search query Sorting:

_request.Sortings.Add(new Sorting(…));

I heard you were discussing your logic with my colleague, add your solution also here for others to see later thanks.

The answer is that it’s not possible to sort on specific TextOption values. It’s only possible to sort on the field it self:

case SearchQueryConstants.Popular:
    var fieldTagName = _fieldDefinitionService.Get<ProductArea>("ItemExtraLabels")?.GetTagName();
    if (fieldTagName != null)
    {
        _request.Sortings.Add(new Sorting(fieldTagName, SortDirection.Descending, SortingFieldType.String));
    }

    break;

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.