Kentico 13 possibly double counting Cart items

Matthew Butler asked on October 26, 2022 10:41

I'm assuming a cart can never have multiple items for the same SKUID. But just unsure on the reason for the following code?

in the UnitPriceCalculator it calculate each cart item, but instead of using item.Quantity, it gets the sum of all items in the cart with the same SKUID, so if 2 items had the same SKUID it would double the calc?

Just not sure for the reasoning of the logic, as there is either 1 and it can use item.Quantity, or theres more and it potentially doubles the result.

Recent Answers


Brenden Kehren answered on October 28, 2022 15:04

The cart is smart enough, even if you have some bad API code, that when you add a given SKU with the same properties or attributes as a SKU that's already in the cart, it just updates the quantity of that SKUCartItem. Keep in mind, ALL of the attributes of that SKU being added to the cart have to be the same (color, size, etc.).

Calling the following shoppingService.AddItemToCart(cartItemParams) should recalculate the cart for you so you can get the totals of the cart.

0 votesVote for this answer Mark as a Correct answer

Matthew Butler answered on October 28, 2022 15:46 (last edited on October 28, 2022 15:50)

Ok, so its just redundant code, the cart will always ensure 1 item per SKUID.

I can just use item.Quantity.

Thanks

        private static decimal GetUnitsCount(CalculationRequestItemitem,IEnumerable<CalculationRequestItem> items)
        {
            return items.Where(cartItem => cartItem.SKU.SKUID == item.SKU.SKUID).Sum(cartItem => cartItem.Quantity);
        }
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 28, 2022 15:48

Yes, with the condition that the SKUID has all the same attributes as the prior SKU.

0 votesVote for this answer Mark as a Correct answer

Matthew Butler answered on October 28, 2022 15:51

What happens if the SKUID has different attributes? does it add a second cart item? If so I think it will double count.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 28, 2022 15:57

Check out the COM_ShoppingCartSKU table. Add some items to the cart and query that table to see the results. You'll find the SKUID and the CartItemParentGUID will be a combination of how things work out. This is very specific if you're using options for products. If you're just using "flat" products without options, then the SKUID will be unique.

When I say flat products I mean something like a hat that is one size fits all, with no color options. Or a set of headphones that are black with no color options.

If you were to have more complex products, it could be hat with youth and adult sizing options as well as 3 different colors. Or a set of headphones with black, silver and red color options as well as Bluetooth or no Bluetooth options.

0 votesVote for this answer Mark as a Correct answer

Matthew Butler answered on October 28, 2022 16:07 (last edited on October 28, 2022 16:07)

Ok so the options effectively just map to separate SKUs, looking at DancingGoat.

So doesn't double count, just redundant code. I'll use item.Quantity.

Thank you

SKUID SKUNumber

61 = HAR-V60-Ceramic-White-2-cups

62 = HAR-V60-Plastic-Red-2-cups

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.