+1-888-365-2779
Try Now
More in this section

Forums / Ecommerce / SortingOptions for the ProductList widget

SortingOptions for the ProductList widget

4 posts, 0 answered
  1. Tim
    Tim  avatar
    34 posts
    Registered:
    16 Oct 2012
    26 Jun 2013
    Link to this post
    1. Is it possible to sort by a custom field that I've added to my products ? (e.g. MostPopular )

    2. Is there a list of possible values to put in the Advanced Settings->SortOder list ?
    PriceAsc:PriceAscendingOptionText, PriceDesc:PriceDescendingOptionText,NameAsc:NameAscendingOptionText, NameDesc:NameDescendingOptionText, CreatedDate:CreatedDateOptionText

    Thanks in advance for any insight,
    Tim
  2. Svetla
    Svetla avatar
    36 posts
    Registered:
    07 Sep 2015
    27 Jun 2013
    Link to this post
    Hi Tim,

    Here is more information on your question:

    For the sorting options, you can add any default fields there, those are basically coma separated key value pairs. The keys(NameAsc etc.) can be names of any of the default fields(any fields that toggle under the default section in your product type).

    The ones you see there basically are short form of the following :

    internal const string CreatedDate = "PublicationDate DESC";
    internal const string PriceAsc = "Price ASC";
    internal const string PriceDesc = "Price DESC";
    internal const string NameAsc = "Title ASC";
    internal const string NameDesc = "Title DESC";


    But there you can add things like Sku DESC etc. The values placed after the colon are just localized strings,  you can add your own in Administration->labels and messages, just make sure their classid is catalog resources.

    As for the custom fields, if you try to add a custom field in there, it will result into an error(null reference) therefore we have an extension point that lets you handle those too. Basically the query for products is being called in a class that implements IProductExpressionFilter. This interface requires only one method (SortAndFilterProducts), that needs to return an IQuerable of your newly sorted/filtered products. What you can do is create a class that implements this interface, here is how this class looks like:

    using System;
    using System.Linq;
    using Telerik.Sitefinity.Data;
    using Telerik.Sitefinity.Ecommerce.Catalog.Model;
    using Telerik.Sitefinity.Modules.Ecommerce.Catalog.Interfaces;
     
    namespace SitefinityWebApp.Widgets
    {
        public class CustomSortFilter : IProductExpressionFilter
        {
            public Tuple<IQueryable<Product>, int?> SortAndFilterProducts(IQueryable<Product> products, int skip, int? take, string filterExpression, string sortExpression)
            {
     
                //this is the default implementation, you can add your logic that checks for the field(s)
                //you need and does the filtering here.
                int? totalCount = 0;
                var filteredProducts = DataProviderBase.SetExpressions(
                   products,
                   filterExpression,
                   sortExpression,
                   skip,
                   take,
                   ref totalCount);
                return new Tuple<IQueryable<Product>, int?>(filteredProducts, totalCount);
            }
        }
    }


    All that's left to do is to do an if-else for custom vs default fields and for the custom ones: build out your own query method and do the sorting appropriately. One thing that I can recommend though is to be really careful with loading items into memory and sorting. With large product sets this can consume large chunks of memory. 

    Since IProductExpressionFilter is IoC bound you can just replace it with your own class through ObjectFactory in global.asax:

    protected void Application_Start(object sender, EventArgs e)
            {
                Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
            }
     
            protected void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs args)
            {
                  
                if (args.CommandName == "Bootstrapped")
                {
     
     
                    ObjectFactory.Container.RegisterType(typeof(IProductExpressionFilter), typeof(CustomSortFilter));
                }
            }

    Hope this helps!


    Best Regards,
    Svetla
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Anthony
    Anthony avatar
    2 posts
    Registered:
    20 Feb 2015
    05 Nov 2015
    Link to this post

    How would I implement this into the backend eCommerce products for sorting a custom field called StartDate. I can't figure out where to reference this code inorder to use it.

     

    Thank you.

  4. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    28 Sep 2016
    28 Dec 2015
    Link to this post
    Hello Anthony,

    For the time being it is not possible to sort the ecommerce products in the backend by custom field. We have logged this as a feature request in our feedback portal on the following link:

    http://feedback.telerik.com/Project/153/Feedback/Details/152369-ability-to-sort-products-in-the-backend-grid-by-custom-field

    You can subscribe to receive notifications for the feature request including new comments, votes, status changes by clicking on the "Follow this item" link. If you would like to unsubscribe, you can unfollow the item by removing "Following this item”.

    You can also vote to increase the popularity of the feature request.

    I also created a KB article related to this:
    http://www.sitefinity.com/developer-network/knowledge-base/details/sorting-products-in-the-backend-grid-by-custom-field

    Regards,
    Sabrie Nedzhip
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
4 posts, 0 answered