/// <summary> /// Gets a shipping discount /// </summary> /// <param name="customer">Customer</param> /// <param name="shippingTotal">Shipping total</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Shipping discount</returns> public static decimal GetShippingDiscount(Customer customer, decimal shippingTotal, out Discount appliedDiscount) { decimal shippingDiscountAmount = decimal.Zero; string customerCouponCode = string.Empty; if (customer != null) customerCouponCode = customer.LastAppliedCouponCode; var allDiscounts = DiscountManager.GetAllDiscounts(DiscountTypeEnum.AssignedToShipping); var allowedDiscounts = new List<Discount>(); foreach (var _discount in allDiscounts) { if (_discount.IsActive(customerCouponCode) && _discount.DiscountType == DiscountTypeEnum.AssignedToShipping && !allowedDiscounts.ContainsDiscount(_discount.Name)) { //discount requirements if (_discount.CheckDiscountRequirements(customer) && _discount.CheckDiscountLimitations(customer)) { allowedDiscounts.Add(_discount); } } } appliedDiscount = DiscountManager.GetPreferredDiscount(allowedDiscounts, shippingTotal); if (appliedDiscount != null) { shippingDiscountAmount = appliedDiscount.GetDiscountAmount(shippingTotal); } if (shippingDiscountAmount < decimal.Zero) shippingDiscountAmount = decimal.Zero; shippingDiscountAmount = Math.Round(shippingDiscountAmount, 2); return shippingDiscountAmount; }
/// <summary> /// Gets an order discount (applied to order total) /// </summary> /// <param name="customer">Customer</param> /// <param name="orderTotal">Order total</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Order discount</returns> public decimal GetOrderTotalDiscount(Customer customer, decimal orderTotal, out Discount appliedDiscount) { decimal discountAmount = decimal.Zero; string customerCouponCode = string.Empty; if (customer != null) customerCouponCode = customer.LastAppliedCouponCode; var allDiscounts = IoC.Resolve<IDiscountService>().GetAllDiscounts(DiscountTypeEnum.AssignedToOrderTotal); var allowedDiscounts = new List<Discount>(); foreach (var discount in allDiscounts) { if (discount.IsActive(customerCouponCode) && discount.DiscountType == DiscountTypeEnum.AssignedToOrderTotal && !allowedDiscounts.ContainsDiscount(discount.Name)) { //discount requirements if (discount.CheckDiscountRequirements(customer) && discount.CheckDiscountLimitations(customer)) { allowedDiscounts.Add(discount); } } } appliedDiscount = IoC.Resolve<IDiscountService>().GetPreferredDiscount(allowedDiscounts, orderTotal); if (appliedDiscount != null) { discountAmount = appliedDiscount.GetDiscountAmount(orderTotal); } if (discountAmount < decimal.Zero) discountAmount = decimal.Zero; discountAmount = Math.Round(discountAmount, 2); return discountAmount; }
/// <summary> /// Gets discount amount /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="customer">The customer</param> /// <param name="additionalCharge">Additional charge</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> public static decimal GetDiscountAmount(ProductVariant productVariant, Customer customer, decimal additionalCharge, out Discount appliedDiscount) { decimal appliedDiscountAmount = decimal.Zero; //we don't apply discounts to products with price entered by a customer if (productVariant.CustomerEntersPrice) { appliedDiscount = null; return appliedDiscountAmount; } appliedDiscount = GetPreferredDiscount(productVariant, customer, additionalCharge); if (appliedDiscount != null) { decimal finalPriceWithoutDiscount = GetFinalPrice(productVariant, customer, additionalCharge, false); appliedDiscountAmount = appliedDiscount.GetDiscountAmount(finalPriceWithoutDiscount); } return appliedDiscountAmount; }