Skip to content

Commit 5932d77

Browse files
authored
VCST-1564: Adds FindExistingLineItemBeforeAdd method (#569)
feat: Adds a new FindExistingLineItemBeforeAdd method. It is responsible for finding an existing line item before adding a new one. If the method returns the line item, the new line item should be merged with the existing one.
1 parent 6d352ab commit 5932d77

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/XPurchase/VirtoCommerce.XPurchase/CartAggregate.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -973,32 +973,46 @@ protected virtual bool CheckPricePolicy(TierPrice tierPrice)
973973
return tierPrice.Price.Amount > 0;
974974
}
975975

976-
protected virtual async Task<CartAggregate> InnerAddLineItemAsync(LineItem lineItem, CartProduct product = null, IList<DynamicPropertyValue> dynamicProperties = null)
976+
protected virtual async Task<CartAggregate> InnerAddLineItemAsync(LineItem newLineItem, CartProduct product = null, IList<DynamicPropertyValue> dynamicProperties = null)
977977
{
978-
var existingLineItem = LineItems.FirstOrDefault(li => li.ProductId == lineItem.ProductId);
978+
var existingLineItem = FindExistingLineItemBeforeAdd(newLineItem.ProductId, product, dynamicProperties);
979+
979980
if (existingLineItem != null)
980981
{
981-
await InnerChangeItemQuantityAsync(existingLineItem, existingLineItem.Quantity + Math.Max(1, lineItem.Quantity), product);
982+
await InnerChangeItemQuantityAsync(existingLineItem, existingLineItem.Quantity + Math.Max(1, newLineItem.Quantity), product);
982983

983-
existingLineItem.FulfillmentCenterId = lineItem.FulfillmentCenterId;
984-
existingLineItem.FulfillmentCenterName = lineItem.FulfillmentCenterName;
984+
existingLineItem.FulfillmentCenterId = newLineItem.FulfillmentCenterId;
985+
existingLineItem.FulfillmentCenterName = newLineItem.FulfillmentCenterName;
985986

986-
lineItem = existingLineItem;
987+
newLineItem = existingLineItem;
987988
}
988989
else
989990
{
990-
lineItem.Id = null;
991-
Cart.Items.Add(lineItem);
991+
newLineItem.Id = null;
992+
Cart.Items.Add(newLineItem);
992993
}
993994

994995
if (dynamicProperties != null)
995996
{
996-
await UpdateCartItemDynamicProperties(lineItem, dynamicProperties);
997+
await UpdateCartItemDynamicProperties(newLineItem, dynamicProperties);
997998
}
998999

9991000
return this;
10001001
}
10011002

1003+
/// <summary>
1004+
/// Responsible for finding an existing line item before adding a new one.
1005+
/// If method returns line item, it means that the new line item should be merged with the existing one.
1006+
/// </summary>
1007+
/// <param name="newProductId">new product id</param>
1008+
/// <param name="newProduct">new product object</param>
1009+
/// <param name="newDynamicProperties">new dynamuc properties that should be added/updated in cart line item</param>
1010+
/// <returns></returns>
1011+
protected virtual LineItem FindExistingLineItemBeforeAdd(string newProductId, CartProduct newProduct, IList<DynamicPropertyValue> newDynamicProperties)
1012+
{
1013+
return LineItems.FirstOrDefault(x => x.ProductId == newProductId);
1014+
}
1015+
10021016
protected virtual void EnsureCartExists()
10031017
{
10041018
if (Cart == null)

0 commit comments

Comments
 (0)