private bool ApplyManufacturerDiscount(DiscountCoupon discountCoupon, Cart cart) { var manufacturerIds = discountCoupon.RestrictionIds(); var cartItemUpdated = false; foreach (var cartItem in cart.CartItems) { if (cartItem.Product.ManufacturerId.HasValue && manufacturerIds.Contains(cartItem.Product.ManufacturerId.Value)) { cartItem.Discount = discountCoupon.GetDiscountAmount(cartItem.Price, cartItem.Quantity); cartItemUpdated = true; } } if (cartItemUpdated && discountCoupon.MaximumDiscountAmount > 0 && cart.CartItems.Sum(x => x.Discount) > discountCoupon.MaximumDiscountAmount) { cart.Discount = discountCoupon.MaximumDiscountAmount; foreach (var cartItem in cart.CartItems) { cartItem.Discount = 0; } } return(cartItemUpdated); }
private bool ApplyVendorDiscount(DiscountCoupon discountCoupon, Cart cart) { var vendorIds = discountCoupon.RestrictionIds().ToArray(); var vendorProductIds = _productService.GetProductIdsByVendorIds(vendorIds); var cartUpdated = false; foreach (var cartItem in cart.CartItems) { if (vendorProductIds.Contains(cartItem.ProductId)) { cartItem.Discount = discountCoupon.GetDiscountAmount(cartItem.Price, cartItem.Quantity); cartUpdated = true; } } if (cartUpdated && discountCoupon.MaximumDiscountAmount > 0 && cart.CartItems.Sum(x => x.Discount) > discountCoupon.MaximumDiscountAmount) { cart.Discount = discountCoupon.MaximumDiscountAmount; foreach (var cartItem in cart.CartItems) { cartItem.Discount = 0; } } return(cartUpdated); }
private bool ApplyRoleDiscount(DiscountCoupon discountCoupon, Cart cart) { var roleIds = discountCoupon.RestrictionIds(); var user = _userService.Get(cart.UserId); if (user.Roles.Any(x => roleIds.Contains(x.Id))) { foreach (var cartItem in cart.CartItems) { cartItem.Discount = discountCoupon.GetDiscountAmount(cartItem.Price, cartItem.Quantity); } if (discountCoupon.MaximumDiscountAmount > 0 && cart.CartItems.Sum(x => x.Discount) > discountCoupon.MaximumDiscountAmount) { cart.Discount = discountCoupon.MaximumDiscountAmount; foreach (var cartItem in cart.CartItems) { cartItem.Discount = 0; } } return(true); } return(false); }
private bool ApplyCategoryDiscount(DiscountCoupon discountCoupon, Cart cart) { var categoryIds = discountCoupon.RestrictionIds().ToArray(); var categoryProductIds = _productService.GetProductIdsByCategoryIds(categoryIds); var cartUpdated = false; foreach (var cartItem in cart.CartItems) { if (categoryProductIds.Contains(cartItem.ProductId)) { cartItem.Discount = discountCoupon.GetDiscountAmount(cartItem.Price, cartItem.Quantity); cartUpdated = true; } } return(cartUpdated); }
private bool ApplyProductDiscount(DiscountCoupon discountCoupon, Cart cart) { var productIds = discountCoupon.RestrictionIds(); var cartItemUpdated = false; foreach (var cartItem in cart.CartItems) { if (!productIds.Contains(cartItem.ProductId)) { continue; } cartItem.Discount = discountCoupon.GetDiscountAmount(cartItem.Price, cartItem.Quantity); cartItem.FinalPrice = cartItem.Price - cartItem.Discount; cartItemUpdated = true; } return(cartItemUpdated); }
private decimal GetProductDiscountedPrice(DiscountCoupon discountCoupon, Product product, ProductVariant variant, User user, int quantity) { var price = variant?.Price ?? product.Price; switch (discountCoupon.RestrictionType) { case RestrictionType.Products: return(discountCoupon.RestrictionIds().Contains(product.Id) ? discountCoupon.GetDiscountAmount(price, quantity) : 0); case RestrictionType.Categories: var categoryIds = discountCoupon.RestrictionIds().ToArray(); var categoryProductIds = _productService.GetProductIdsByCategoryIds(categoryIds); return(categoryProductIds.Contains(product.Id) ? discountCoupon.GetDiscountAmount(price, quantity) : 0); case RestrictionType.Users: return(discountCoupon.RestrictionIds().Contains(user.Id) ? discountCoupon.GetDiscountAmount(price, quantity) : 0); case RestrictionType.UserGroups: return(0); case RestrictionType.Roles: var roleIds = discountCoupon.RestrictionIds(); return(user.Roles.Any(x => roleIds.Contains(x.Id)) ? discountCoupon.GetDiscountAmount(price, quantity) : 0); case RestrictionType.Vendors: var vendorIds = discountCoupon.RestrictionIds().ToArray(); var vendorProductIds = _productService.GetProductIdsByVendorIds(vendorIds); return(vendorProductIds.Contains(product.Id) ? discountCoupon.GetDiscountAmount(price, quantity) : 0); case RestrictionType.Manufacturers: return(product.ManufacturerId.HasValue && discountCoupon.RestrictionIds().Contains(product.ManufacturerId.Value) ? discountCoupon.GetDiscountAmount(price, quantity) : 0); default: return(0); } }