public CommerceInstance GetCurrentInstance() { var httpContext = GetHttpContext(); if (httpContext == null) { return(null); } var instance = httpContext.Items["Kooboo.Commerce.Data.CommerceInstance.Current"] as CommerceInstance; if (instance == null) { var instanceName = GetInstanceName(GetHttpContext()); if (!String.IsNullOrWhiteSpace(instanceName)) { instance = _instanceManager.GetInstance(instanceName); httpContext.Items["Kooboo.Commerce.Data.CommerceInstance.Current"] = instance; } } return(instance); }
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 CommerceInstance GetInstance() { return(_instanceManager.GetInstance(InstanceName)); }