Skip to content

Commit

Permalink
Add products to line items on cart refresh (#42)
Browse files Browse the repository at this point in the history
* Load products to line items on cart refresh

* add product to lineItem on cart refresh

* add response groups

* fix bug with new product adding
  • Loading branch information
vkrashenko authored and tatarincev committed Dec 22, 2017
1 parent e3dbce4 commit 041c7a6
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ 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);
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 = products.FirstOrDefault(x => x.Id == lineItem.ProductId);
lineItem.PaymentPlan = paymentPlans.FirstOrDefault(x => x.Id == lineItem.ProductId);
}
}
Expand Down Expand Up @@ -124,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);
}
}
Expand Down Expand Up @@ -301,6 +305,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)
Expand Down Expand Up @@ -408,12 +413,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();
Expand Down Expand Up @@ -479,30 +482,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));
Expand Down Expand Up @@ -562,10 +562,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)
{
Expand Down Expand Up @@ -604,12 +603,5 @@ protected virtual void EnsureCartExists()
throw new StorefrontException("Cart not loaded.");
}
}

protected virtual async Task<Product[]> GetCartProductsAsync()
{
var productIds = Cart.Items.Select(i => i.ProductId).ToArray();
var products = await _catalogService.GetProductsAsync(productIds, ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithDiscounts | ItemResponseGroup.Inventory);
return products;
}
}
}

0 comments on commit 041c7a6

Please sign in to comment.