/// <summary> /// Gets discounts applied to entity /// </summary> /// <typeparam name="T">Type based on <see cref="DiscountMapping" /></typeparam> /// <param name="entity">Entity which supports discounts (<see cref="IDiscountSupported{T}" />)</param> /// <returns>List of discounts</returns> public virtual IList <Discount> GetAppliedDiscounts <T>(IDiscountSupported <T> entity) where T : DiscountMapping { var _discountMappingRepository = EngineContext.Current.Resolve <IRepository <T> >(); return((from d in _discountRepository.Table join ad in _discountMappingRepository.Table on d.Id equals ad.DiscountId where ad.EntityId == entity.Id select d).ToList()); }
/// <summary> /// Prepare selected and all available discounts for the passed model by entity applied discounts /// </summary> /// <typeparam name="TModel">Discount supported model type</typeparam> /// <typeparam name="TMapping">Discount supported entity type</typeparam> /// <param name="model">Model</param> /// <param name="entity">Entity</param> /// <param name="availableDiscounts">List of all available discounts</param> /// <param name="ignoreAppliedDiscounts">Whether to ignore existing applied discounts</param> public virtual void PrepareModelDiscounts <TModel, TMapping>(TModel model, IDiscountSupported <TMapping> entity, IList <Discount> availableDiscounts, bool ignoreAppliedDiscounts) where TModel : IDiscountSupportedModel where TMapping : DiscountMapping { if (model == null) { throw new ArgumentNullException(nameof(model)); } //prepare already applied discounts if (!ignoreAppliedDiscounts && entity != null) { model.SelectedDiscountIds = _discountService.GetAppliedDiscounts(entity).Select(discount => discount.Id).ToList(); } PrepareModelDiscounts(model, availableDiscounts); }
/// <summary> /// Gets discounts applied to entity /// </summary> /// <typeparam name="T">Type based on <see cref="DiscountMapping" /></typeparam> /// <param name="entity">Entity which supports discounts (<see cref="IDiscountSupported{T}" />)</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the list of discounts /// </returns> public virtual async Task <IList <Discount> > GetAppliedDiscountsAsync <T>(IDiscountSupported <T> entity) where T : DiscountMapping { var discountMappingRepository = EngineContext.Current.Resolve <IRepository <T> >(); var cacheKey = _staticCacheManager.PrepareKeyForShortTermCache(NopDiscountDefaults.AppliedDiscountsCacheKey, entity.GetType().Name, entity); var appliedDiscounts = await _staticCacheManager.GetAsync(cacheKey, async() => { return(await(from d in _discountRepository.Table join ad in discountMappingRepository.Table on d.Id equals ad.DiscountId where ad.EntityId == entity.Id select d).ToListAsync()); }); return(appliedDiscounts); }