Try our conversational search powered by Generative AI!

EntryAmount correct in SerializableCarts, but not populated/zero in lineItem

Vote:
 

I am debugging on my local environment why _lineItemCalculator.GetDiscountedPrice(lineItem, cart.Currency); returns 0 for a discounted item. The calculator is the default one(DefaultLineItemCalculator). When I check in the database the entry amount is correctly set:

LineItems:
          - LineItemId: -1
            Code: 86658
            DisplayName: Varmelampe 1500 W
            PlacedPrice: 989.000000000
            Quantity: 1.0
            Properties: {}
            EntryAmount: 179.000000000
            TaxCategoryId: 1
            SalesTax: 162.00
            IsSalesTaxUpToDate: true

But in the code, i.e.

cart.GetAllLineItems().First()

shows zero.

I also notice that 'IsSalesTaxUpToDate' is false so it seems some extended properties are set to defaults. 

#299204
Mar 29, 2023 12:07
Vote:
 

Found it... it looked fishy that at one point cart was properly populated(_checkoutCalculator.GetTotals), but at another not(GetCartItem), but the trick was that GetCartItem is just a delegate so there's no guarantees when it gets evaluated. A simple .ToList() at the end of the .Select() solved it.

protected virtual ICartResponse CreateCartResponse<TResponseType>(ICart cart, IPrincipal user, IDictionary<ILineItem, List<ValidationIssue>> validationIssues = null, IList<CouponCodeValidation> couponCodeValidationIssues = null) where TResponseType : ICartResponse, new()
        {
            if (cart == null)
            {
                return CreateEmptyCartResponse<TResponseType>();
            }

            List<ILineItem> list = cart.GetAllLineItems().ToList();
            List<RewardDescription> list2 = (from r in ApplyDiscounts(cart).ToList()
                                             where r.AppliedCoupon != null
                                             select r).ToList();
            List<CartItemValidation> removedLineitemValidationIssues = GetRemovedLineitemValidationIssues(list, validationIssues);
            OrderTotals totals = _checkoutCalculator.GetTotals(cart);
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            if (cart.Properties != null)
            {
                foreach (object key in cart.Properties.Keys)
                {
                    dictionary.Add(key.ToString(), cart.Properties[key]);
                }
            }

            return new TResponseType
            {
                AppliedCouponCodes = list2.Select((RewardDescription r) => CreateCouponCodeResponse(cart, r)),
                CouponCodeValidationIssues = GetCouponCodeValidations(list2, couponCodeValidationIssues?.ToList()),
                Items = list.Select((ILineItem li) => GetCartItem(cart, li, validationIssues?.FirstOrDefault((KeyValuePair<ILineItem, List<ValidationIssue>> x) => x.Key.Code.Equals(li.Code)).Value)),
                Currency = cart.Currency.CurrencyCode,
                TotalQuantity = list.Sum((ILineItem li) => li.Quantity),
                SubTotalPrice = totals.SubTotalPrice,
                DiscountTotalPrice = totals.DiscountTotalPrice,
                TaxTotal = totals.TaxTotal,
                TotalPrice = totals.TotalPrice,
                TotalPriceWithoutTax = totals.TotalPriceWithoutTax,
                ShipmentTotalPrice = totals.ShipmentTotalPrice,
                ShipmentDiscountPrice = totals.ShipmentDiscountPrice,
                ShipmentTaxTotal = totals.ShipmentTaxTotal,
                ValidationIssues = removedLineitemValidationIssues,
                Properties = dictionary,
                Name = cart.Name,
                Notes = cart.Notes.Select((IOrderNote x) => CreateOrderNoteResponse(cart, x))
            };
        }
#299206
Mar 29, 2023 13:49
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.