Skip to content

Commit

Permalink
fix: discount for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev committed Dec 16, 2024
1 parent c0b006d commit c38b75d
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public class CreateConfiguredLineItemCommand : ICommand<ExpConfigurationLineItem

public int Quantity { get; set; } = 1;

public bool EvaluatePromotions { get; set; } = false;

public IList<ProductConfigurationSection> ConfigurationSections { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void UpdatePrice(LineItem lineItem)
lineItem.ListPrice = _items.Select(x => x.Item).Sum(x => x.ListPrice * x.Quantity) + configurableProductPrice.ListPrice.Amount;
lineItem.SalePrice = _items.Select(x => x.Item).Sum(x => x.SalePrice * x.Quantity) + configurableProductPrice.SalePrice.Amount;

lineItem.DiscountAmount = Math.Max(0, lineItem.ListPrice - lineItem.SalePrice);
lineItem.DiscountAmount = configurableProductPrice.DiscountAmount.Amount;
lineItem.PlacedPrice = lineItem.ListPrice - lineItem.DiscountAmount;
lineItem.ExtendedPrice = lineItem.PlacedPrice * lineItem.Quantity;
}
Expand Down
1 change: 1 addition & 0 deletions src/VirtoCommerce.XCart.Core/Models/CartProductsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class CartProductsRequest

public bool LoadPrice { get; set; } = true;
public bool LoadInventory { get; set; } = true;
public bool EvaluatePromotions { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface ICartProductsLoaderService
/// </summary>
/// <param name="request">Request (cart data to use, product ids)</param>
/// <returns>List of <see cref="CartProduct"/></returns>
Task<IList<CartProduct>> GetCartProductsByIdsAsync(CartProductsRequest request);
Task<IList<CartProduct>> GetCartProductsAsync(CartProductsRequest request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override Task BeforeMediatorSend(IResolveFieldContext<object> context,
{
request.UserId = context.GetCurrentUserId();
request.OrganizationId = context.GetCurrentOrganizationId();
request.EvaluatePromotions = true;

return base.BeforeMediatorSend(context, request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public async Task<ExpConfigurationLineItem> Handle(CreateConfiguredLineItemComma

var productsRequest = container.GetCartProductsRequest();
productsRequest.ProductIds = new[] { request.ConfigurableProductId };
productsRequest.EvaluatePromotions = request.EvaluatePromotions;

var product = (await _cartProductService.GetCartProductsByIdsAsync(productsRequest)).FirstOrDefault();
var product = (await _cartProductService.GetCartProductsAsync(productsRequest)).FirstOrDefault();
if (product == null)
{
throw new OperationCanceledException($"Product with id {request.ConfigurableProductId} not found");
Expand All @@ -45,7 +46,9 @@ public async Task<ExpConfigurationLineItem> Handle(CreateConfiguredLineItemComma

productsRequest.ProductIds = selectedProductIds;
productsRequest.LoadInventory = false;
var products = await _cartProductService.GetCartProductsByIdsAsync(productsRequest);
productsRequest.EvaluatePromotions = false; // don't need to evaluate promotions for the selected products

var products = await _cartProductService.GetCartProductsAsync(productsRequest);

foreach (var section in request.ConfigurationSections)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<ProductConfigurationQueryResponse> Handle(GetProductConfigurat

var productsRequest = container.GetCartProductsRequest();
productsRequest.ProductIds = allProductIds;
var cartProducts = await _cartProductService.GetCartProductsByIdsAsync(productsRequest);
var cartProducts = await _cartProductService.GetCartProductsAsync(productsRequest);

var productByIds = cartProducts.ToDictionary(x => x.Product.Id, x => x);

Expand Down
21 changes: 19 additions & 2 deletions src/VirtoCommerce.XCart.Data/Services/CartProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public async Task<IList<CartProduct>> GetCartProductsByIdsAsync(CartAggregate ag
/// </summary>
/// <param name="request">Request</param>
/// <returns>List of <see cref="CartProduct"/>s</returns>
public async Task<IList<CartProduct>> GetCartProductsByIdsAsync(CartProductsRequest request)
public async Task<IList<CartProduct>> GetCartProductsAsync(CartProductsRequest request)
{
if (request is null || request.ProductIds.IsNullOrEmpty())
{
return new List<CartProduct>();
}

var cartProducts = await GetCartProductsAsync(request.ProductIds, request.Store.Id, request.Currency.Code, request.UserId, request.ProductsIncludeFields ?? IncludeFields);
var cartProducts = await LoadCartProductsAsync(request);

var productsToLoadDependencies = cartProducts.Where(x => x.LoadDependencies).ToList();
if (productsToLoadDependencies.Count != 0)
Expand Down Expand Up @@ -141,6 +141,23 @@ protected virtual async Task<List<CartProduct>> GetCartProductsAsync(IList<strin
return result;
}

protected virtual async Task<List<CartProduct>> LoadCartProductsAsync(CartProductsRequest request)
{
var productsQuery = new LoadProductsQuery
{
UserId = request.UserId,
StoreId = request.Store.Id,
CurrencyCode = request.Currency.Code,
ObjectIds = request.ProductIds,
IncludeFields = request.ProductsIncludeFields ?? IncludeFields,
EvaluatePromotions = request.EvaluatePromotions,
};

var response = await _mediator.Send(productsQuery);
var result = response.Products.Select(x => new CartProduct(x)).ToList();
return result;
}

/// <summary>
/// Load all properties for <see cref="CartProduct"/>s
/// </summary>
Expand Down

0 comments on commit c38b75d

Please sign in to comment.