private IEnumerable <IGrouping <int, CartItem> > GroupCartItemsByShippingAddress(CartItemCollection cartItems, int defaultShippingAddressId) { return(cartItems .Select(ci => new { ShippingAddressId = ci.ShippingAddressID > 0 ? ci.ShippingAddressID : defaultShippingAddressId, CartItems = ci, }) .GroupBy(o => o.ShippingAddressId, o => o.CartItems)); }
private string GetCartHash(CartItemCollection cartItems, Customer customer, IEnumerable <OrderOption> orderOptions) { var originAddress = GetOriginAddress(); string cartComposite = cartItems .Select(c => new { CartItem = c, Address = new Address(c.ShippingAddressID) }) .Aggregate( String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_{9}_{10}", originAddress.City, originAddress.Region, originAddress.PostalCode, customer.PrimaryShippingAddress.City, customer.PrimaryShippingAddress.State, customer.PrimaryShippingAddress.Zip, customer.LevelHasNoTax, cartItems.DiscountResults.Sum(dr => dr.OrderTotal), cartItems.DiscountResults.Sum(dr => dr.ShippingTotal), cartItems.DiscountResults.Sum(dr => dr.LineItemTotal), orderOptions.Aggregate("_", (s, oo) => String.Format("{0}_{1}_{2}_{3}", s, oo.ID, oo.TaxClassID, oo.Cost))), (s, o) => String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}", s, o.CartItem.ShoppingCartRecordID, o.CartItem.VariantID, o.CartItem.Quantity, o.CartItem.Price, o.CartItem.IsTaxable, o.CartItem.TaxClassID, o.CartItem.ShippingAddressID, o.CartItem.ShippingMethodID) ); using (var md5 = System.Security.Cryptography.MD5CryptoServiceProvider.Create()) { var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(cartComposite)); return(hash.Aggregate(String.Empty, (s, b) => String.Format("{0}{1:x2}", s, b))); } }