/// <summary> /// Gets shopping cart total /// </summary> /// <param name="cart">Cart</param> /// <param name="appliedGiftCards">Applied gift cards</param> /// <param name="discountAmount">Applied discount amount</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="redeemedRewardPoints">Reward points to redeem</param> /// <param name="redeemedRewardPointsAmount">Reward points amount in primary store currency to redeem</param> /// <param name="ignoreRewardPonts">A value indicating whether we should ignore reward points (if enabled and a customer is going to use them)</param> /// <param name="usePaymentMethodAdditionalFee">A value indicating whether we should use payment method additional fee when calculating order total</param> /// <returns>Shopping cart total;Null if shopping cart total couldn't be calculated now</returns> public virtual decimal? GetShoppingCartTotal(IList<ShoppingCartItem> cart, out decimal discountAmount, out Discount appliedDiscount, out List<AppliedGiftCard> appliedGiftCards, out int redeemedRewardPoints, out decimal redeemedRewardPointsAmount, bool ignoreRewardPonts = false, bool usePaymentMethodAdditionalFee = true) { redeemedRewardPoints = 0; redeemedRewardPointsAmount = decimal.Zero; var customer = cart.GetCustomer(); string paymentMethodSystemName = ""; if (customer != null) { paymentMethodSystemName = customer.GetAttribute<string>( SystemCustomerAttributeNames.SelectedPaymentMethod, _genericAttributeService, _storeContext.CurrentStore.Id); } //subtotal without tax decimal subtotalBase = decimal.Zero; decimal orderSubTotalDiscountAmount = decimal.Zero; Discount orderSubTotalAppliedDiscount = null; decimal subTotalWithoutDiscountBase = decimal.Zero; decimal subTotalWithDiscountBase = decimal.Zero; GetShoppingCartSubTotal(cart, false, out orderSubTotalDiscountAmount, out orderSubTotalAppliedDiscount, out subTotalWithoutDiscountBase, out subTotalWithDiscountBase); //subtotal with discount subtotalBase = subTotalWithDiscountBase; //shipping without tax decimal? shoppingCartShipping = GetShoppingCartShippingTotal(cart, false); //payment method additional fee without tax decimal paymentMethodAdditionalFeeWithoutTax = decimal.Zero; if (usePaymentMethodAdditionalFee && !String.IsNullOrEmpty(paymentMethodSystemName)) { decimal paymentMethodAdditionalFee = _paymentService.GetAdditionalHandlingFee(cart, paymentMethodSystemName); paymentMethodAdditionalFeeWithoutTax = _taxService.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, false, customer); } //tax decimal shoppingCartTax = GetTaxTotal(cart, usePaymentMethodAdditionalFee); //order total decimal resultTemp = decimal.Zero; resultTemp += subtotalBase; if (shoppingCartShipping.HasValue) { resultTemp += shoppingCartShipping.Value; } resultTemp += paymentMethodAdditionalFeeWithoutTax; resultTemp += shoppingCartTax; if (_shoppingCartSettings.RoundPricesDuringCalculation) resultTemp = Math.Round(resultTemp, 2); #region Order total discount discountAmount = GetOrderTotalDiscount(customer, resultTemp, out appliedDiscount); //sub totals with discount if (resultTemp < discountAmount) discountAmount = resultTemp; //reduce subtotal resultTemp -= discountAmount; if (resultTemp < decimal.Zero) resultTemp = decimal.Zero; if (_shoppingCartSettings.RoundPricesDuringCalculation) resultTemp = Math.Round(resultTemp, 2); #endregion #region Applied gift cards //let's apply gift cards now (gift cards that can be used) appliedGiftCards = new List<AppliedGiftCard>(); if (!cart.IsRecurring()) { //we don't apply gift cards for recurring products var giftCards = _giftCardService.GetActiveGiftCardsAppliedByCustomer(customer); if (giftCards!=null) foreach (var gc in giftCards) if (resultTemp > decimal.Zero) { decimal remainingAmount = gc.GetGiftCardRemainingAmount(); decimal amountCanBeUsed = decimal.Zero; if (resultTemp > remainingAmount) amountCanBeUsed = remainingAmount; else amountCanBeUsed = resultTemp; //reduce subtotal resultTemp -= amountCanBeUsed; var appliedGiftCard = new AppliedGiftCard(); appliedGiftCard.GiftCard = gc; appliedGiftCard.AmountCanBeUsed = amountCanBeUsed; appliedGiftCards.Add(appliedGiftCard); } } #endregion if (resultTemp < decimal.Zero) resultTemp = decimal.Zero; if (_shoppingCartSettings.RoundPricesDuringCalculation) resultTemp = Math.Round(resultTemp, 2); decimal? orderTotal = null; if (!shoppingCartShipping.HasValue) { //return null if we have errors orderTotal = null; return orderTotal; } else { //return result if we have no errors orderTotal = resultTemp; } #region Reward points if (_rewardPointsSettings.Enabled && !ignoreRewardPonts && customer.GetAttribute<bool>(SystemCustomerAttributeNames.UseRewardPointsDuringCheckout, _genericAttributeService, _storeContext.CurrentStore.Id)) { int rewardPointsBalance = customer.GetRewardPointsBalance(); if (CheckMinimumRewardPointsToUseRequirement(rewardPointsBalance)) { decimal rewardPointsBalanceAmount = ConvertRewardPointsToAmount(rewardPointsBalance); if (orderTotal.HasValue && orderTotal.Value > decimal.Zero) { if (orderTotal.Value > rewardPointsBalanceAmount) { redeemedRewardPoints = rewardPointsBalance; redeemedRewardPointsAmount = rewardPointsBalanceAmount; } else { redeemedRewardPointsAmount = orderTotal.Value; redeemedRewardPoints = ConvertAmountToRewardPoints(redeemedRewardPointsAmount); } } } } #endregion if (orderTotal.HasValue) { orderTotal = orderTotal.Value - redeemedRewardPointsAmount; if (_shoppingCartSettings.RoundPricesDuringCalculation) orderTotal = Math.Round(orderTotal.Value, 2); return orderTotal; } else return null; }
/// <summary> /// Gets shopping cart total /// </summary> /// <param name="cart">Cart</param> /// <param name="appliedGiftCards">Applied gift cards</param> /// <param name="discountAmount">Applied discount amount</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="redeemedRewardPoints">Reward points to redeem</param> /// <param name="redeemedRewardPointsAmount">Reward points amount in primary store currency to redeem</param> /// <param name="ignoreRewardPonts">A value indicating whether we should ignore reward points (if enabled and a customer is going to use them)</param> /// <returns>Shopping cart total;Null if shopping cart total couldn't be calculated now</returns> public virtual decimal?GetShoppingCartTotal(IList <ShoppingCartItem> cart, out decimal discountAmount, out Discount appliedDiscount, out List <AppliedGiftCard> appliedGiftCards, out int redeemedRewardPoints, out decimal redeemedRewardPointsAmount, bool ignoreRewardPonts = false) { redeemedRewardPoints = 0; redeemedRewardPointsAmount = decimal.Zero; var customer = cart.GetCustomer(); string paymentMethodSystemName = ""; if (customer != null) { paymentMethodSystemName = customer.SelectedPaymentMethodSystemName; } //subtotal without tax decimal subtotalBase = decimal.Zero; decimal orderSubTotalDiscountAmount = decimal.Zero; Discount orderSubTotalAppliedDiscount = null; decimal subTotalWithoutDiscountBase = decimal.Zero; decimal subTotalWithDiscountBase = decimal.Zero; GetShoppingCartSubTotal(cart, false, out orderSubTotalDiscountAmount, out orderSubTotalAppliedDiscount, out subTotalWithoutDiscountBase, out subTotalWithDiscountBase); //subtotal with discount subtotalBase = subTotalWithDiscountBase; //shipping without tax decimal?shoppingCartShipping = GetShoppingCartShippingTotal(cart, false); //payment method additional fee without tax decimal paymentMethodAdditionalFeeWithoutTax = decimal.Zero; if (!String.IsNullOrEmpty(paymentMethodSystemName)) { decimal paymentMethodAdditionalFee = _paymentService.GetAdditionalHandlingFee(paymentMethodSystemName); paymentMethodAdditionalFeeWithoutTax = _taxService.GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, false, customer); } //tax decimal shoppingCartTax = GetTaxTotal(cart); //order total decimal resultTemp = decimal.Zero; resultTemp += subtotalBase; if (shoppingCartShipping.HasValue) { resultTemp += shoppingCartShipping.Value; } resultTemp += paymentMethodAdditionalFeeWithoutTax; resultTemp += shoppingCartTax; if (_shoppingCartSettings.RoundPricesDuringCalculation) { resultTemp = Math.Round(resultTemp, 2); } #region Order total discount discountAmount = GetOrderTotalDiscount(customer, resultTemp, out appliedDiscount); //sub totals with discount if (resultTemp < discountAmount) { discountAmount = resultTemp; } //reduce subtotal resultTemp -= discountAmount; if (resultTemp < decimal.Zero) { resultTemp = decimal.Zero; } if (_shoppingCartSettings.RoundPricesDuringCalculation) { resultTemp = Math.Round(resultTemp, 2); } #endregion #region Applied gift cards //let's apply gift cards now (gift cards that can be used) appliedGiftCards = new List <AppliedGiftCard>(); if (!cart.IsRecurring()) { //we don't apply gift cards for recurring products var giftCards = _giftCardService.GetActiveGiftCardsAppliedByCustomer(customer); if (giftCards != null) { foreach (var gc in giftCards) { if (resultTemp > decimal.Zero) { decimal remainingAmount = gc.GetGiftCardRemainingAmount(); decimal amountCanBeUsed = decimal.Zero; if (resultTemp > remainingAmount) { amountCanBeUsed = remainingAmount; } else { amountCanBeUsed = resultTemp; } //reduce subtotal resultTemp -= amountCanBeUsed; var appliedGiftCard = new AppliedGiftCard(); appliedGiftCard.GiftCard = gc; appliedGiftCard.AmountCanBeUsed = amountCanBeUsed; appliedGiftCards.Add(appliedGiftCard); } } } } #endregion if (resultTemp < decimal.Zero) { resultTemp = decimal.Zero; } if (_shoppingCartSettings.RoundPricesDuringCalculation) { resultTemp = Math.Round(resultTemp, 2); } decimal?orderTotal = null; if (!shoppingCartShipping.HasValue) { //return null if we have errors orderTotal = null; return(orderTotal); } else { //return result if we have no errors orderTotal = resultTemp; } #region Reward points if (_rewardPointsSettings.Enabled && customer != null && customer.UseRewardPointsDuringCheckout && !ignoreRewardPonts) { int rewardPointsBalance = customer.GetRewardPointsBalance(); decimal rewardPointsBalanceAmount = ConvertRewardPointsToAmount(rewardPointsBalance); if (orderTotal.HasValue && orderTotal.Value > decimal.Zero) { if (orderTotal.Value > rewardPointsBalanceAmount) { redeemedRewardPoints = rewardPointsBalance; redeemedRewardPointsAmount = rewardPointsBalanceAmount; } else { redeemedRewardPointsAmount = orderTotal.Value; redeemedRewardPoints = ConvertAmountToRewardPoints(redeemedRewardPointsAmount); } } } #endregion if (orderTotal.HasValue) { orderTotal = orderTotal.Value - redeemedRewardPointsAmount; if (_shoppingCartSettings.RoundPricesDuringCalculation) { orderTotal = Math.Round(orderTotal.Value, 2); } return(orderTotal); } else { return(null); } }