1. Jeffrey Olmstead
  2. Nwazet.Commerce

Commits

ems  committed e32abc5

Fixes shopping cart controller for multiple products in shopping cart with different attributes and also minor bugs elsewhere.

  • Participants
  • Parent commits 2f39116
  • Branches default

Comments (0)

Files changed (4)

File Controllers/ShoppingCartController.cs

View file
  • Ignore whitespace
             if (items == null)
                 return;
 
-            var itemQuantities = GetItemQuantities(items);
-
+            var minimumOrderQuantities = GetMinimumOrderQuantities(items);
+            
             _shoppingCart.AddRange(
                 items
                     .Where(item => !item.IsRemoved)
                     .Select(item => new ShoppingCartItem(
                                         item.ProductId,
-                                        item.Quantity <= 0 ? 0 : itemQuantities[item.ProductId],
+                                        item.Quantity <= 0 ? 0 : item.Quantity < minimumOrderQuantities[item.ProductId] ? minimumOrderQuantities[item.ProductId] : item.Quantity,                                        
                                         item.AttributeIdsToValues))
                 );
 
                 });
         }
 
-        private Dictionary<int, int> GetItemQuantities(IEnumerable<UpdateShoppingCartItemViewModel> items) {
-            Dictionary<int, int> minimumItemQuantites = new Dictionary<int, int>();
+        private Dictionary<int, int> GetMinimumOrderQuantities(IEnumerable<UpdateShoppingCartItemViewModel> items) {
+            Dictionary<int, int> minimumOrderQuantites = new Dictionary<int, int>();
             int defaultMinimumQuantity = 1;
 
             if (items != null) {
-                var productIds = items.Select(i => i.ProductId);
+                var productIds = items.Select(i => i.ProductId).Distinct();
                 var products = _contentManager.GetMany<ProductPart>(productIds, VersionOptions.Published, QueryHints.Empty).ToList();
 
                 // Because a product might not exist (may be unpublished or deleted but still in shopping cart) need to iterate instead of using .ToDictionary
                 foreach (var item in items) {
-                    var product = products.Where(p => p.Id == item.ProductId).FirstOrDefault();
-
-                    if (product != null) {
-                        minimumItemQuantites.Add(product.Id, item.Quantity < product.MinimumOrderQuantity ? product.MinimumOrderQuantity : item.Quantity);
-                    }
-                    else {
-                        // This ensures the dictionary will have all the keys needed for the items
-                        minimumItemQuantites.Add(item.ProductId, item.Quantity < defaultMinimumQuantity ? defaultMinimumQuantity : item.Quantity);
+                    // Check to ensure productId doesn't exist (can happen when product in cart multiple times with different attributes
+                    if (!minimumOrderQuantites.ContainsKey(item.ProductId)) {
+                        var product = products.Where(p => p.Id == item.ProductId).FirstOrDefault();
+                        if (product != null) {
+                            minimumOrderQuantites.Add(product.Id, product.MinimumOrderQuantity);
+                        }
+                        else {
+                            // This ensures the dictionary will have all the keys needed for the items
+                            minimumOrderQuantites.Add(item.ProductId, defaultMinimumQuantity);
+                        }
                     }
                 }
             }
-            
-            return minimumItemQuantites;
+
+            return minimumOrderQuantites;
         }
 
         public ActionResult ResetDestination() {

File Drivers/DiscountPartDriver.cs

View file
  • Ignore whitespace
             var model = new DiscountEditorViewModel();
             if (updater.TryUpdateModel(model, Prefix, null, null)) {
                 part.Name = model.Name;
-                part.Record.Discount = model.Discount;
+                string discountString = model.Discount;
+                double percent;
+                double discount;
+                if (!String.IsNullOrEmpty(discountString)) {
+                    if (discountString.Trim().EndsWith("%")) {
+                        if (double.TryParse(discountString.Substring(0, discountString.Length - 1), out percent)) {
+                            part.DiscountPercent = percent;
+                        }
+                    }
+                    else {
+                        if (double.TryParse(discountString, out discount)) {
+                            part.Discount = discount;
+                        }
+                    }
+                }
                 var currentCulture = Services.WorkContext.CurrentCulture;
                 var cultureInfo = CultureInfo.GetCultureInfo(currentCulture);
                 var timeZone = Services.WorkContext.CurrentTimeZone;

File Models/ShoppingCart.cs

View file
  • Ignore whitespace
                 _contentManager.GetMany<ProductPart>(ids, VersionOptions.Published,
                 new QueryHints().ExpandParts<TitlePart, ProductPart, AutoroutePart>()).ToArray();
 
+            var productPartIds = productParts.Select(p => p.Id);
+
             var shoppingCartQuantities =
-                (from item in Items
+                (from item in Items  
+                 where productPartIds.Contains(item.ProductId)
                  select new ShoppingCartQuantityProduct(item.Quantity, productParts.First(p => p.Id == item.ProductId), item.AttributeIdsToValues))
                     .ToList();
 

File Services/BundleService.cs

View file
  • Ignore whitespace
             _bundleProductsRepository.Create(
                 new BundleProductsRecord {
                     BundlePartRecord = record,
-                    ContentItemRecord = _contentManager.Get(product).Record,
+                    ContentItemRecord = _contentManager.Get(product, VersionOptions.Latest).Record,
                     Quantity = quantity
                 });
         }