コード例 #1
0
        private void LoadSearchResults()
        {
            if (!this.Performed)
            {
                return;
            }

            var pageSize = this.Context == null ? 20 : this.Context["paginate.page_size"].ToInt(20);
            var skip     = this.Context == null ? 0 : this.Context["paginate.current_offset"].ToInt();
            var terms    = this.Terms; //this.Context["current_query"] as string;
            var type     = this.Context == null ? "product" : this.Context["current_type"] as string;

            var siteContext = SiteContext.Current;
            var service     = CommerceService.Create();
            var searchQuery = new BrowseQuery()
            {
                Skip = skip, Take = pageSize, Search = terms
            };
            var response = Task.Run(() => service.SearchAsync <object>(siteContext, searchQuery)).Result;

            this.Results = response;

            this.Performed = false;
        }
コード例 #2
0
        public static string T(string input, params object[] variables)
        {
            var service     = CommerceService.Create();
            var context     = SiteContext.Current;
            var locs        = service.GetLocale(context);
            var defaultLocs = service.GetLocale(context, true);

            if (locs == null && defaultLocs == null)
            {
                return(input);
            }

            string retVal;

            // first get a template string
            if (variables != null && variables.Length > 0)
            {
                var dictionary =
                    variables.Where(x => (x is Tuple <string, object>))
                    .Select(x => x as Tuple <string, object>)
                    .ToDictionary(x => x.Item1, x => x.Item2);

                string template;
                if (dictionary.ContainsKey("count") && dictionary["count"] != null) // execute special count routing
                {
                    var    count = dictionary["count"].ToInt();
                    JToken templateToken;
                    switch (count)
                    {
                    case 1:
                        templateToken = locs.GetValue(defaultLocs, input + ".one");
                        break;

                    case 0:
                        templateToken = locs.GetValue(defaultLocs, input + ".zero", null);
                        break;

                    case 2:
                        templateToken = locs.GetValue(defaultLocs, input + ".two", null);
                        break;

                    default:
                        templateToken = locs.GetValue(defaultLocs, input + ".other");
                        break;
                    }

                    if (templateToken == null)
                    {
                        templateToken = locs.GetValue(defaultLocs, input + ".other");
                        template      = templateToken != null?templateToken.ToString() : String.Empty;
                    }
                    else
                    {
                        template = templateToken.ToString();
                    }
                }
                else
                {
                    template = locs.GetValue(defaultLocs, input);
                }

                var templateEngine = Template.Parse(template);
                retVal = templateEngine.Render(Hash.FromDictionary(dictionary));
            }
            else
            {
                retVal = locs.GetValue(defaultLocs, input);
            }

            return(retVal);
        }
コード例 #3
0
        public void LoadSlice(int from, int?to)
        {
            var pageSize = to == null ? 50 : to - from;

            var tags = this.Context["current_tags"] as SelectedTagCollection;

            var filters = new Dictionary <string, string[]>();

            if (tags != null && tags.Any())
            {
                // split tags to field=value using "_", if there is no "_", then simply add them to "tags"=values
                var tagsMultiArray = tags.Select(t => t.Split(new[] { '_' }));

                var tagsArray = new List <Tuple <string, string> >();

                // add tags that have "_"
                tagsArray.AddRange(tagsMultiArray.Where(x => x.Length > 1).Select(x => new Tuple <string, string>(x[0], x[1])));

                // add the rest that don't have "_" as tags, will sort them out on the server api
                tagsArray.AddRange(tagsMultiArray.Where(x => x.Length == 1).Select(x => new Tuple <string, string>("tags", x[0])));

                foreach (var tagsGroup in tagsArray.GroupBy(x => x.Item1))
                {
                    filters.Add(tagsGroup.Key, tagsGroup.Select(g => g.Item2).ToArray());
                }
            }

            var service = CommerceService.Create();
            var context = SiteContext.Current;

            var sortProperty  = String.Empty;
            var sortDirection = "ascending";

            var sort = string.IsNullOrEmpty(this.SortBy) ? this.DefaultSortBy : this.SortBy;

            if (sort.Equals("manual"))
            {
                sortProperty = "position";
            }
            else if (sort.Equals("best-selling"))
            {
                sortProperty = "position";
            }
            else
            {
                var sortArray = sort.Split(new[] { '-' });
                if (sortArray.Length > 1)
                {
                    sortProperty  = sortArray[0];
                    sortDirection = sortArray[1];
                }
            }

            var searchQuery = new BrowseQuery()
            {
                SortProperty = sortProperty, SortDirection = sortDirection, Filters = filters, Skip = from, Take = pageSize.Value, Outline = Id == "All" ? "" : this.BuildSearchOutline()
            };
            var response =
                Task.Run(() => service.SearchAsync <Product>(context,
                                                             searchQuery, this, responseGroups: ItemResponseGroups.ItemSmall | ItemResponseGroups.Variations)).Result;

            // populate tags with facets returned
            if (response.Facets != null && response.Facets.Any())
            {
                var values = response.Facets.SelectMany(f => f.Values.Select(v => v.AsWebModel(f.Field)));
                this.Tags = new TagCollection(values);
            }

            this.AllProductsCount = response.TotalCount;

            this.Products = response;
        }