Skip to content

Commit

Permalink
VCST-2473: order cart line items (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Jan 23, 2025
1 parent 2cf8de4 commit 63f5d03
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/VirtoCommerce.XCart.Core/CartAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public virtual async Task<CartAggregate> AddConfiguredItemAsync(NewCartItem newC
newConfiguredItem.Quantity = newCartItem.Quantity;
newConfiguredItem.Note = newCartItem.Comment;

await UpdateCreatedDate(newConfiguredItem, newCartItem);

Cart.Items.Add(newConfiguredItem);

if (newCartItem.DynamicProperties != null)
Expand Down Expand Up @@ -238,6 +240,8 @@ public virtual async Task<CartAggregate> AddItemAsync(NewCartItem newCartItem)
lineItem.SelectedForCheckout = newCartItem.IsSelectedForCheckout ?? IsSelectedForCheckout;
lineItem.Quantity = newCartItem.Quantity;

await UpdateCreatedDate(lineItem, newCartItem);

if (newCartItem.Price != null)
{
lineItem.ListPrice = newCartItem.Price.Value;
Expand Down Expand Up @@ -920,6 +924,16 @@ public virtual async Task<CartAggregate> UpdateOrganizationName()
return this;
}

public virtual Task<CartAggregate> UpdateCreatedDate(LineItem lineItem, NewCartItem newCartItem)
{
if (newCartItem.CreatedDate != null)
{
lineItem.CreatedDate = newCartItem.CreatedDate.Value;
}

return Task.FromResult(this);
}

public virtual async Task<CartAggregate> UpdateCartDynamicProperties(IList<DynamicPropertyValue> dynamicProperties)
{
await _dynamicPropertyUpdaterService.UpdateDynamicPropertyValues(Cart, dynamicProperties);
Expand Down
3 changes: 3 additions & 0 deletions src/VirtoCommerce.XCart.Core/Commands/AddCartItemCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using VirtoCommerce.Xapi.Core.Models;
using VirtoCommerce.XCart.Core.Commands.BaseCommands;
Expand Down Expand Up @@ -29,5 +30,7 @@ public class AddCartItemCommand : CartCommand
/// Configurable product sections
/// </summary>
public IList<ProductConfigurationSection> ConfigurationSections { get; set; }

public DateTime? CreatedDate { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/VirtoCommerce.XCart.Core/Models/NewCartItem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using VirtoCommerce.Xapi.Core.Models;

Expand Down Expand Up @@ -37,5 +38,10 @@ public NewCartItem(string productId, int quantity)
public bool? IsSelectedForCheckout { get; set; }

public bool IgnoreValidationErrors { get; set; }

/// <summary>
/// LineItem created date override
/// </summary>
public DateTime? CreatedDate { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/VirtoCommerce.XCart.Core/Schemas/CartType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ await cartAvailMethods.GetAvailableGiftsAsync(context.Source)
// Items
ExtendableField<NonNullGraphType<ListGraphType<NonNullGraphType<LineItemType>>>>("items",
"Items",
resolve: context => context.Source.LineItems);
resolve: context => context.Source.LineItems.OrderByDescending(x => x.CreatedDate));

Field<NonNullGraphType<IntGraphType>>("itemsCount",
"Item count",
Expand Down
2 changes: 2 additions & 0 deletions src/VirtoCommerce.XCart.Core/Schemas/InputAddItemType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public InputAddItemType()

// Configurable product support
Field<ListGraphType<ConfigurationSectionInput>>("configurationSections");

Field<DateTimeGraphType>("createdDate", "Create date. Optional, to manually control line item position in the cart if required. ISO-8601 format, for example: 2025-01-23T11:46:11Z");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public override async Task<CartAggregate> Handle(AddCartItemCommand request, Can
DynamicProperties = request.DynamicProperties,
Price = request.Price,
CartProduct = product,
CreatedDate = request.CreatedDate,
};

var configurations = await _productConfigurationSearchService.SearchNoCloneAsync(new ProductConfigurationSearchCriteria
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected virtual async Task CopyItems(CartAggregate currentCurrencyCartAggregat
.Select(x => new NewCartItem(x.ProductId, x.Quantity)
{
IgnoreValidationErrors = true,
CreatedDate = x.CreatedDate,
Comment = x.Note,
IsSelectedForCheckout = x.SelectedForCheckout,
DynamicProperties = x.DynamicProperties.SelectMany(x => x.Values.Select(y => new DynamicPropertyValue()
Expand Down Expand Up @@ -128,6 +129,7 @@ await newCurrencyCartAggregate.AddConfiguredItemAsync(new NewCartItem(configurat
{
CartProduct = contaner.ConfigurableProduct,
IgnoreValidationErrors = true,
CreatedDate = configurationLineItem.CreatedDate,
Comment = configurationLineItem.Note,
IsSelectedForCheckout = configurationLineItem.SelectedForCheckout,
DynamicProperties = configurationLineItem.DynamicProperties.SelectMany(x => x.Values.Select(y => new DynamicPropertyValue()
Expand Down

0 comments on commit 63f5d03

Please sign in to comment.