private static IReadOnlyList <PaymentShippingOption> CreateShippingOptions(ShoppingCart shoppingCart) { List <PaymentShippingOption> paymentShippingOptions = new List <PaymentShippingOption>(); ShippingType selectedShippingType = shoppingCart.ShippingType; IReadOnlyDictionary <ShippingType, ShoppingCartCostsSummary> cartShippingOptions = shoppingCart.CalculateShippingOptions(); ShoppingCartCostsSummary costsOfSelectedShipping = cartShippingOptions[selectedShippingType]; foreach (var kvp in cartShippingOptions) { ShippingType shippingType = kvp.Key; ShoppingCartCostsSummary costs = kvp.Value; PaymentShippingOption shippingOption = new PaymentShippingOption( label: ShippingTypeStringUtilities.CreateShippingOptionTitle(shippingType, costs, costsOfSelectedShipping), selected: (shippingType == selectedShippingType), tag: shippingType.ToString(), amount: CreateCurrencyAmount(costs.Shipping)); paymentShippingOptions.Add(shippingOption); } return(paymentShippingOptions); }
/// <summary> /// Updates the strings that display the cost summary (e.g. sub-total, tax, etc.) of the shopping cart. /// </summary> private void UpdateCostsSummaryStrings() { ShoppingCartCostsSummary costsSummary = this.ShoppingCart.CostsSummary; // Update values. this.SubtotalString = PriceStringUtilities.CreatePriceString(costsSummary.ItemsSubtotal); this.EstimatedShippingString = PriceStringUtilities.CreatePriceString(costsSummary.Shipping); this.ShippingTypeString = ShippingTypeStringUtilities.GetFriendlyNameOfShippingType(this.ShoppingCart.ShippingType); this.EstimatedTaxString = PriceStringUtilities.CreatePriceString(costsSummary.TotalTax); this.TotalCostString = PriceStringUtilities.CreatePriceString(costsSummary.Total); // Raise all the 'PropertyChanged' events. this.RaisePropertyChanged(nameof(this.SubtotalString)); this.RaisePropertyChanged(nameof(this.EstimatedShippingString)); this.RaisePropertyChanged(nameof(this.ShippingTypeString)); this.RaisePropertyChanged(nameof(this.EstimatedTaxString)); this.RaisePropertyChanged(nameof(this.TotalCostString)); }
/// <summary> /// Create the friendly name of a shipping option (including cost). /// </summary> public static string CreateShippingOptionTitle(ShippingType type, ShoppingCartCostsSummary costs, ShoppingCartCostsSummary currentTotalCost) { string shippingName = GetFriendlyNameOfShippingType(type); if (costs.Total == currentTotalCost.Total) { return(shippingName); } else { decimal costDifference = costs.Total - currentTotalCost.Total; if (costDifference >= 0) { return($"{shippingName}: +{PriceStringUtilities.CreatePriceString(costDifference)}"); } else { return($"{shippingName}: -{PriceStringUtilities.CreatePriceString(-costDifference)}"); } } }
/// <summary> /// Calculate the summary of costs (e.g. sub-total, tax, etc.) for the given list of shopping cart entries. /// </summary> private static ShoppingCartCostsSummary CalculateCostsSummary(IReadOnlyList <ShoppingCartEntry> entries, ShippingType shippingType, PaymentAddress shippingAddress) { ShoppingCartCostsSummary costsSummary = new ShoppingCartCostsSummary(); foreach (ShoppingCartEntry entry in entries) { costsSummary.ItemsSubtotal += entry.Quantity * entry.Product.Cost; costsSummary.Shipping += entry.Product.ShippingCost.Costs[shippingType]; } costsSummary.Shipping += ShippingFixedCosts.Costs[shippingType]; CalculateTax( shippingAddress, costsSummary.ItemsSubtotal, costsSummary.Shipping, out costsSummary.ItemsTax, out costsSummary.ShippingTax); costsSummary.TotalTax = costsSummary.ItemsTax + costsSummary.ShippingTax; costsSummary.Total = costsSummary.ItemsSubtotal + costsSummary.Shipping + costsSummary.TotalTax; return(costsSummary); }