nopSolutions / nopCommerce

ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart.
https://www.nopcommerce.com
Other
9.14k stars 5.25k forks source link

An item with the same key has already been added. Key: Tracking Number in GetShippingAddressAsync #7128

Closed danFbach closed 4 months ago

danFbach commented 4 months ago

nopCommerce version: 4.60.6

Towards the end of the GetShippingAddressAsync function in PdfService there is:

var shipments = await _shipmentService.GetShipmentsByOrderIdAsync(order.Id);
foreach (var shipment in shipments)
{
    addressResult.CustomValues.Add("Tracking Number", shipment.TrackingNumber);
}

Wouldn't this inherently cause an error if there is multiple shipments? CustomValues is a Dictionary<string, object>, so if there is > 1 shipment, you will try adding the Tracking Number key again, and throw System.ArgumentException: An item with the same key has already been added. Key: Tracking Number. Perhaps this should be a List<KeyValuePair<string, object>> instead? I am currently encountering this while generating an invoice with multiple shipments.

danFbach commented 4 months ago

Otherwise you could do something like:

var shipments = await _shipmentService.GetShipmentsByOrderIdAsync(order.Id);
if(shipments.Any())
{
    addressResult.CustomValues.Add("Tracking Number(s)", string.Join(",", shipments.Select(s => s.TrackingNumber)));
}

To keep the model unchanged.

skoshelev commented 4 months ago

Hi @danFbach. I'm very sorry, it looks like this is your code, since in the function you mentioned, as well as in the entire service, I could not find this problem area, here is a link to the method in version 4.60.6

danFbach commented 4 months ago

shoot, you're right, forgot I added that 🤦‍♂️