From 82682c288c5daba33ebb09690ad3088bafad7f48 Mon Sep 17 00:00:00 2001 From: Valeriy Krashenko Date: Fri, 15 Dec 2017 15:34:37 +0200 Subject: [PATCH 1/4] Load products to line items on cart refresh --- .../Cart/AddCartItems.cs | 16 ++++++++++++++++ .../Domain/Cart/CartBuilder.cs | 1 + 2 files changed, 17 insertions(+) create mode 100644 VirtoCommerce.Storefront.Model/Cart/AddCartItems.cs diff --git a/VirtoCommerce.Storefront.Model/Cart/AddCartItems.cs b/VirtoCommerce.Storefront.Model/Cart/AddCartItems.cs new file mode 100644 index 000000000..dd5f12481 --- /dev/null +++ b/VirtoCommerce.Storefront.Model/Cart/AddCartItems.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace VirtoCommerce.Storefront.Model.Cart +{ + public partial class AddCartItems + { + [JsonProperty("id")] + public string ProductIds { get; set; } + [JsonProperty("quantity")] + public int Quantity { get; set; } + public AddCartItems() + { + Quantity = Quantity; + } + } +} diff --git a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs index 7512852d4..7a9661a3a 100644 --- a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs +++ b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs @@ -88,6 +88,7 @@ public virtual async Task LoadOrCreateNewTransientCartAsync(string cartName, Sto cart.PaymentPlan = paymentPlans.FirstOrDefault(x => x.Id == cart.Id); foreach (var lineItem in cart.Items) { + lineItem.Product = (await _catalogService.GetProductsAsync(new[] { lineItem.ProductId }, ItemResponseGroup.ItemWithPrices)).FirstOrDefault(); lineItem.PaymentPlan = paymentPlans.FirstOrDefault(x => x.Id == lineItem.ProductId); } } From 2a844b192960396f63d7f0b6861adca49e7629e6 Mon Sep 17 00:00:00 2001 From: Valeriy Krashenko Date: Fri, 15 Dec 2017 17:31:36 +0200 Subject: [PATCH 2/4] add product to lineItem on cart refresh --- .../Domain/Cart/CartBuilder.cs | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs index 7a9661a3a..e036344e5 100644 --- a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs +++ b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs @@ -82,13 +82,15 @@ public virtual async Task LoadOrCreateNewTransientCartAsync(string cartName, Sto //Load cart payment plan with have same id if (store.SubscriptionEnabled) { + var productsIds = cart.Items.Select(x => x.ProductId).Distinct().ToArray(); + var products = await _catalogService.GetProductsAsync(productsIds, ItemResponseGroup.ItemWithPrices); var paymentPlanIds = new[] { cart.Id }.Concat(cart.Items.Select(x => x.ProductId).Distinct()).ToArray(); - var paymentPlans = await _subscriptionService.GetPaymentPlansByIdsAsync(paymentPlanIds); cart.PaymentPlan = paymentPlans.FirstOrDefault(x => x.Id == cart.Id); + //Realize this code whith dictionary foreach (var lineItem in cart.Items) { - lineItem.Product = (await _catalogService.GetProductsAsync(new[] { lineItem.ProductId }, ItemResponseGroup.ItemWithPrices)).FirstOrDefault(); + lineItem.Product = products.FirstOrDefault(x => x.Id == lineItem.ProductId); lineItem.PaymentPlan = paymentPlans.FirstOrDefault(x => x.Id == lineItem.ProductId); } } @@ -302,6 +304,7 @@ public virtual async Task FillFromQuoteRequestAsync(QuoteRequest quoteRequest) if (quoteItem != null) { var lineItem = product.ToLineItem(Cart.Language, (int)quoteItem.SelectedTierPrice.Quantity); + lineItem.Product = product; lineItem.ListPrice = quoteItem.ListPrice; lineItem.SalePrice = quoteItem.SelectedTierPrice.Price; if (lineItem.ListPrice < lineItem.SalePrice) @@ -409,12 +412,10 @@ public virtual async Task EvaluatePromotionsAsync() { //Get product inventory to fill InStockQuantity parameter of LineItem //required for some promotions evaluation - var products = await GetCartProductsAsync(); foreach (var lineItem in Cart.Items.ToList()) { - var product = products.FirstOrDefault(p => p.Id == lineItem.ProductId); - lineItem.InStockQuantity = (int)product.AvailableQuantity; + lineItem.InStockQuantity = (int)lineItem.Product.AvailableQuantity; } var evalContext = Cart.ToPromotionEvaluationContext(); @@ -480,30 +481,27 @@ protected virtual ShoppingCart CreateCart(string cartName, Store store, User use protected virtual async Task ValidateCartItemsAsync() { - var products = await GetCartProductsAsync(); - foreach (var lineItem in Cart.Items.ToList()) { lineItem.ValidationErrors.Clear(); - var product = products.FirstOrDefault(p => p.Id == lineItem.ProductId); - if (product == null || !product.IsActive || !product.IsBuyable) + if (lineItem.Product == null || !lineItem.Product.IsActive || !lineItem.Product.IsBuyable) { lineItem.ValidationErrors.Add(new UnavailableError()); lineItem.IsValid = false; } else { - var isProductAvailable = new ProductIsAvailableSpecification(product).IsSatisfiedBy(lineItem.Quantity); + var isProductAvailable = new ProductIsAvailableSpecification(lineItem.Product).IsSatisfiedBy(lineItem.Quantity); if (!isProductAvailable) { lineItem.IsValid = false; - var availableQuantity = product.AvailableQuantity; + var availableQuantity = lineItem.Product.AvailableQuantity; lineItem.ValidationErrors.Add(new QuantityError(availableQuantity)); } - var tierPrice = product.Price.GetTierPrice(lineItem.Quantity); + var tierPrice = lineItem.Product.Price.GetTierPrice(lineItem.Quantity); if (tierPrice.Price > lineItem.SalePrice) { lineItem.ValidationErrors.Add(new PriceError(lineItem.SalePrice, lineItem.SalePriceWithTax, tierPrice.Price, tierPrice.PriceWithTax)); @@ -563,10 +561,9 @@ protected virtual async Task ChangeItemQuantityAsync(LineItem lineItem, int quan { if (lineItem != null && !lineItem.IsReadOnly) { - var product = (await _catalogService.GetProductsAsync(new[] { lineItem.ProductId }, ItemResponseGroup.ItemWithPrices)).FirstOrDefault(); - if (product != null) + if (lineItem.Product != null) { - lineItem.SalePrice = product.Price.GetTierPrice(quantity).Price; + lineItem.SalePrice = lineItem.Product.Price.GetTierPrice(quantity).Price; //List price should be always greater ot equals sale price because it may cause incorrect totals calculation if (lineItem.ListPrice < lineItem.SalePrice) { @@ -605,12 +602,5 @@ protected virtual void EnsureCartExists() throw new StorefrontException("Cart not loaded."); } } - - protected virtual async Task GetCartProductsAsync() - { - var productIds = Cart.Items.Select(i => i.ProductId).ToArray(); - var products = await _catalogService.GetProductsAsync(productIds, ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory); - return products; - } } } From dcf7f9743654c74606f7e05f9c0d8cecd2564536 Mon Sep 17 00:00:00 2001 From: Valeriy Krashenko Date: Wed, 20 Dec 2017 10:59:16 +0200 Subject: [PATCH 3/4] add response groups --- VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs index e036344e5..ae0f7dae6 100644 --- a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs +++ b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs @@ -83,7 +83,7 @@ public virtual async Task LoadOrCreateNewTransientCartAsync(string cartName, Sto if (store.SubscriptionEnabled) { var productsIds = cart.Items.Select(x => x.ProductId).Distinct().ToArray(); - var products = await _catalogService.GetProductsAsync(productsIds, ItemResponseGroup.ItemWithPrices); + var products = await _catalogService.GetProductsAsync(productsIds, ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory); var paymentPlanIds = new[] { cart.Id }.Concat(cart.Items.Select(x => x.ProductId).Distinct()).ToArray(); var paymentPlans = await _subscriptionService.GetPaymentPlansByIdsAsync(paymentPlanIds); cart.PaymentPlan = paymentPlans.FirstOrDefault(x => x.Id == cart.Id); From 5dac609c1dda17144b94fb3ed00da9ad315c8e33 Mon Sep 17 00:00:00 2001 From: Valeriy Krashenko Date: Thu, 21 Dec 2017 12:44:23 +0200 Subject: [PATCH 4/4] fix bug with new product adding --- VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs index ae0f7dae6..ac93f5117 100644 --- a/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs +++ b/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs @@ -79,8 +79,8 @@ public virtual async Task LoadOrCreateNewTransientCartAsync(string cartName, Sto var cartDto = cartSearchResult.Results.FirstOrDefault(); var cart = cartDto?.ToShoppingCart(currency, language, user) ?? CreateCart(cartName, store, user, language, currency); - //Load cart payment plan with have same id - if (store.SubscriptionEnabled) + //Load cart payment plan with have same id + if (store.SubscriptionEnabled) { var productsIds = cart.Items.Select(x => x.ProductId).Distinct().ToArray(); var products = await _catalogService.GetProductsAsync(productsIds, ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory); @@ -127,6 +127,7 @@ public virtual async Task AddItemAsync(Product product, int quantity) if (isProductAvailable) { var lineItem = product.ToLineItem(Cart.Language, quantity); + lineItem.Product = product; await AddLineItemAsync(lineItem); } }