Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add products to line items on cart refresh #42

Merged
merged 5 commits into from
Dec 22, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to load products with these response groups
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 @@ -301,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)
Expand Down Expand Up @@ -408,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();
Expand Down Expand Up @@ -479,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));
Expand Down Expand Up @@ -562,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)
{
Expand Down Expand Up @@ -604,12 +602,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;
}
}
}