Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MB 273 posts 936 karma points
    Dec 11, 2016 @ 15:50
    MB
    0

    Count products in price range (dictionary)

    Hello fellow Umbraco'ers,

    I'm working on www.roldskov.sp34k.dk as an online product catalogue. Right now I'm working on a sort of price filtering but I can't seem to figure out how to complete it.

    If you look here you'll notice some price ranges on the left sidebar.

    What functionality I'm trying to go for

    What I wish to accomplish is to count how many products there is in a price range and if there is none, then it shouldn't be displayed.

    Sidebare filtering is made like this

       <ul id="priceFilter" class="section sidebarList">
                    <li @(String.IsNullOrEmpty(selectedpriceRange) ? currentClass : "")>
                        <a href="@CurrentPage.Url@queryStringBuilder("priceRange", "")">Alle priser</a>
                    </li>
                    @{  var priceRanges = new Dictionary<int, int>();
                        priceRanges.Add(0, 4999);
                        priceRanges.Add(5000, 9999);
                        priceRanges.Add(10000, 14999);
                        priceRanges.Add(15000, 24999);
                        priceRanges.Add(25000, 34999);
                        priceRanges.Add(35000, 44999);
                        priceRanges.Add(45000, 54999);
                        priceRanges.Add(55000, 95000);
                    }
                    @foreach (KeyValuePair<int, int> range in priceRanges)
                    {
                        <li>
                            <a href="@CurrentPage.Url@queryStringBuilder("priceRange", string.Format("{0}-{1}", range.Key, range.Value))">
                                @string.Format("{0} - {1}", range.Key, range.Value)
                            </a>
                        </li>
                    }
                </ul>
    

    Products are being written out like this:

        <div id="categoryCollection">
                        @if (!string.IsNullOrEmpty(selectedpriceRange) && selectedpriceRange.Contains("-"))
                        {
                            string[] priceArray = selectedpriceRange.Split('-');
                            int minPrice = 0;
                            int maxPrice = 0;
                            if (priceArray.Count() == 2 && int.TryParse(priceArray[0], out minPrice) && int.TryParse(priceArray[1], out maxPrice))
                            {
                                selectedItems.AddRange(productTypes
                                    .Where(x => x.HasValue("price") &&
                                    x.GetPropertyValue<int>("price") > minPrice &&
                                    x.GetPropertyValue<int>("price") < maxPrice)
                                    .Skip((page - 1) * pageSize).Take(pageSize));
    
                                foreach (var item in selectedItems.Skip((page - 1) * pageSize).Take(pageSize))
                                {
                                    @buildItemProduct(item);
                                }
                            }
                        }
                        else
                        {
                            foreach (IPublishedContent item in productTypes.Skip((page - 1) * pageSize).Take(pageSize))
                            {
    
                                @buildItemProduct(item)
                            }
    
                        }
                    </div>
    
  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Dec 12, 2016 @ 16:49
    Frans de Jong
    1

    Hi Mike,

    If "alle priser" is selected I would loop through all products and check what pricerange they are in and put those in a list. After you looped through the products you can show the filters in the list.

    Frans

  • MB 273 posts 936 karma points
    Dec 20, 2016 @ 13:35
    MB
    0

    Hey Frans,

    I apologize for the slow response. My exame has been taking most of my time.

    Anyways! Could I trick you into showing an example if that isn't too complicated? I'm not sure how to achieve it tbh. I'm still learning :o)

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Dec 20, 2016 @ 20:21
    Søren Kottal
    100

    Hi Mike

    You could do something like this:

                @foreach (KeyValuePair<int, int> range in priceRanges)
                {
                    // assuming you have productTypes variable available
                    var productCount = productTypes.Count(x => x.HasValue("price") && x.GetPropertyValue<int>("price") > range.Key&& x.GetPropertyValue<int>("price") < range.Value);
                    <li>
                        <a href="@CurrentPage.Url@queryStringBuilder("priceRange", string.Format("{0}-{1}", range.Key, range.Value))">
                            @string.Format("{0} - {1} ", range.Key, range.Value)
                            @productCount @(productCount != 1 ? "products" : "product")
                        </a>
                    </li>
                }
    
  • MB 273 posts 936 karma points
    Dec 21, 2016 @ 10:13
    MB
    0

    Hey Søren,

    Thank you for the example, it was exactly what I needed to move forth!

    Thank you once again! :)

Please Sign in or register to post replies

Write your reply to:

Draft