protected override object DoExecute(CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings) { var cartId = 0; var user = context.HttpContext.GetMembershipUser(); if (user == null) { var sessionIdProvider = EngineContext.Current.Resolve <IShoppingCartSessionIdProvider>(); cartId = context.Site.Commerce().ShoppingCarts.GetCartIdBySessionId(sessionIdProvider.GetCurrentSessionId(true)); } else { cartId = context.Site.Commerce().ShoppingCarts.GetCartIdByCustomer(user.Email); } var query = context.Site.Commerce().ShoppingCarts.Query().ById(cartId); if (settings.Includes != null && settings.Includes.Count > 0) { foreach (var path in settings.Includes) { query.Include(path); } } return(query.FirstOrDefault()); }
protected override object DoExecute(CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings) { var query = Query(context.Site.Commerce()); // Filters query = ApplyFilters(query, settings.Filters, context); // Sorting if (!String.IsNullOrEmpty(settings.SortField)) { query.Sorts.Add(new Sort(settings.SortField, settings.SortDirection)); } // Includes if (settings.Includes != null && settings.Includes.Count > 0) { foreach (var path in settings.Includes) { query.Include(path); } } if (settings.TakeOperation == TakeOperation.First) { return(query.FirstOrDefault()); } // Pagination if (settings.EnablePaging) { var pageSize = settings.PageSize.GetValueOrDefault(50); var pageNumber = settings.PageNumber.GetValueOrDefault(1); query.Skip((pageNumber - 1) * pageSize).Take(pageSize); } else { if (settings.Top != null && settings.Top.Value > 0) { query.Take(settings.Top.Value); } } return(query.ToList()); }
protected override object DoExecute(CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings) { var user = context.HttpContext.GetMembershipUser(); if (user == null) { return(null); } var query = context.Site.Commerce().Customers.Query().ByEmail(user.Email); if (settings.Includes != null) { foreach (var include in settings.Includes) { query.Include(include); } } return(query.FirstOrDefault()); }
protected override object DoExecute(CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings) { int?currentCategoryId = null; var productFilter = settings.Filters.Find(f => f.Name == "ByCurrentProduct"); if (productFilter != null) { var productId = (int)productFilter.ParameterValues["CurrentProductId"]; var product = context.Site.Commerce().Products.Query().ById(productId).Include(p => p.Categories).FirstOrDefault(); if (product != null) { var category = product.Categories.FirstOrDefault(); if (category != null) { currentCategoryId = category.Id; } } } if (currentCategoryId == null) { var categoryFilter = settings.Filters.Find(f => f.Name == "ByCurrentCategory"); if (categoryFilter != null) { currentCategoryId = (int)categoryFilter.ParameterValues["CurrentCategoryId"]; } } if (currentCategoryId == null) { return(null); } return(context.Site.Commerce().Categories.Breadcrumb(currentCategoryId.Value)); }
protected override object DoExecute(CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings) { var productIdFilter = settings.Filters.Find(f => f.Name == "ByProduct"); if (productIdFilter == null) { return(null); } var productId = productIdFilter.GetParameterValue("productId"); if (productId == null) { return(null); } var instanceName = context.Site.GetCommerceInstanceName(); if (String.IsNullOrWhiteSpace(instanceName)) { throw new InvalidOperationException("Commerce instance name is not configured in CMS."); } using (var instance = _instanceManager.GetInstance(instanceName)) using (var scope = Scope.Begin(instance)) { var accessories = ProductAccessoryService().GetAccessories((int)productId); if (!String.IsNullOrEmpty(settings.SortField) && settings.SortField == "Rank") { if (settings.SortDirection == SortDirection.Asc) { accessories = accessories.OrderBy(r => r.Rank).ToList(); } else { accessories = accessories.OrderByDescending(r => r.Rank).ToList(); } } if (settings.Top != null) { accessories = accessories.Take(settings.Top.Value).ToList(); } var accessoryIds = accessories.Select(x => x.ProductId).ToArray(); var result = new List <Kooboo.Commerce.Api.Products.Product>(); foreach (var id in accessoryIds) { // TODO: should not use resolve, use ApiService instead var model = EngineContext.Current.Resolve <Kooboo.Commerce.Api.Products.IProductApi>() .Query() .ById(id) .Include("PriceList") .Include("Images") .Include("Brand") .FirstOrDefault(); if (model != null) { result.Add(model); } } return(result); } }
private IRecommendationEngine GetRecommendationEngine(CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings, out ISet <string> toIgnoreItems) { var filter = settings.Filters.Find(f => f.Name == "ByProduct"); if (filter != null) { var productId = (int)filter.GetParameterValue("ProductId"); toIgnoreItems = new HashSet <string> { productId.ToString() }; return(new FeatureBasedRecommendationEngine(new[] { new Feature(productId.ToString()) }, RelatedItemsProviders.GetProviders(context.Instance))); } else { toIgnoreItems = new HashSet <string>(); foreach (var behaviorType in BehaviorTypes.All()) { var store = BehaviorStores.Get(context.Instance, behaviorType); foreach (var itemId in store.GetItemsUserHadBehaviorsOn(context.HttpContext.EnsureVisitorUniqueId(), 10000)) { toIgnoreItems.Add(itemId); } } return(RecommendationEngines.GetEngines(context.Instance)); } }
protected override object DoExecute(Commerce.CMSIntegration.DataSources.CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings) { var userId = context.HttpContext.GetVisitorUniqueId(); var top = settings.Top.GetValueOrDefault(4); ISet <string> toIgnoreItems; var engine = GetRecommendationEngine(context, settings, out toIgnoreItems); var items = engine.Recommend(userId, top, toIgnoreItems); var itemIds = items.Select(it => Convert.ToInt32(it.ItemId)).ToArray(); var reasonItemIds = new List <int>(); foreach (var item in items) { var reasonId = item.GetBestReasonItemId(); if (reasonId > 0) { reasonItemIds.Add(reasonId); } } var products = context.Site.Commerce() .Products.Query().ByIds(itemIds) .Include(p => p.Brand) .Include(p => p.Images) .Include(p => p.Variants) .ToList(); var reasons = context.Site.Commerce() .Products.Query().ByIds(reasonItemIds.ToArray()) .ToList(); var result = new List <ProductRecommendation>(); foreach (var item in items) { var productId = Convert.ToInt32(item.ItemId); var product = products.Find(p => p.Id == productId); if (product != null) { var recommendation = new ProductRecommendation { Product = product, Weight = item.Weight }; var reasonId = item.GetBestReasonItemId(); if (reasonId > 0) { recommendation.Reason = reasons.Find(p => p.Id == reasonId); } result.Add(recommendation); } } return(result); }