private bool ApplyShippingDiscount(DiscountCoupon discountCoupon, Cart cart) { var discount = discountCoupon.GetDiscountAmount(cart.ShippingFee, 1); cart.Discount = discount; return(true); }
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 ApplyOrderSubTotalDiscount(DiscountCoupon discountCoupon, Cart cart) { var subTotal = GetOrderSubTotal(cart); cart.Discount = discountCoupon.GetDiscountAmount(subTotal, 1); return(true); }
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 ApplyPaymentMethodDiscount(DiscountCoupon discountCoupon, Cart cart) { var paymentMethodNames = discountCoupon.RestrictionValues(); if (paymentMethodNames.Contains(cart.PaymentMethodName)) { var paymentHandler = PluginHelper.GetPaymentHandler(cart.PaymentMethodName); var discount = discountCoupon.GetDiscountAmount(paymentHandler.GetPaymentHandlerFee(cart), 1); if (discountCoupon.HasCouponCode) { cart.Discount = discount; } cart.PaymentMethodFee = cart.PaymentMethodFee - discount; return(true); } return(false); }
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 bool ApplyOrderTotalDiscount(DiscountCoupon discountCoupon, Cart cart) { var orderTotalForDiscount = decimal.Zero; var otherOrderTotal = decimal.Zero; foreach (var cartItem in cart.CartItems) { if (discountCoupon.ExcludeAlreadyDiscountedProducts && cartItem.Discount > 0) { otherOrderTotal += cartItem.Tax + cartItem.Price * cartItem.Quantity; continue; } orderTotalForDiscount += cartItem.Tax + cartItem.Price * cartItem.Quantity; } cart.Discount = discountCoupon.GetDiscountAmount(orderTotalForDiscount, 1); return(true); }
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); } }