Total Price not updating after adding items to shopping cart and checking out

Charlie Bodnar-Horvath asked on June 1, 2017 05:42

Hi,

I'm relatively new to Kentico and I'm trying to create a shopping cart, add items to it and then checkout/create an order. I don't want to show the cart on the page so I'm doing it with the API. I'm using the pages here and here in this regard. I can't seem to get the total price to update when I add a product to an order. This is part of what i've tried so far:

int productId = SKUInfoProvider.GetSKUs().FirstObject.SKUID;
SKUInfo product = SKUInfoProvider.GetSKUInfo(productId);

 ShoppingCartInfo cart = new ShoppingCartInfo
 {
     ShoppingCartBillingAddress = orderBillingAddress,
     ShoppingCartShippingAddress = orderShippingAddress,
     TotalPrice = 0,
     TotalTax = 0,
     ShoppingCartCustomerID = customer.CustomerID,
     ShoppingCartSiteID = SiteContext.CurrentSiteID,
 };

 ShoppingCartInfoProvider.SetShoppingCartInfo(cart);

After setting up the cart, I want to add an item(just a test product I've created). The product exists and I can add it directly to an order but for some strange reason it doesn't update the total price of the order which is stuck at 0.

 ShoppingCartItemParameters parameters = new ShoppingCartItemParameters(product.SKUID, 1);
 ShoppingCartItemInfo cartItem = cart.SetShoppingCartItem(parameters);

 ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);

 OrderInfo newOrder = new OrderInfo
 {
   OrderInvoiceNumber = "1",
   OrderBillingAddress = orderBillingAddress,
   OrderShippingAddress = orderShippingAddress,
   OrderTotalPrice = cartItem.UnitPrice * cartItem.CartItemUnits,
   OrderTotalTax = 0,
   OrderDate = DateTime.Now,
   OrderStatusID = newStatus.StatusID,
   OrderCustomerID = customer.CustomerID,
   OrderSiteID = SiteContext.CurrentSiteID
 };

 OrderInfoProvider.SetOrderInfo(newOrder);


 OrderItemInfo newItem = new OrderItemInfo
 {
    OrderItemSKUName = cartItem.SKU.SKUName,
    OrderItemOrderID = newOrder.OrderID,
    OrderItemSKUID = cartItem.SKUID,
    OrderItemUnitPrice = cartItem.SKU.SKUPrice,
    OrderItemUnitCount = cartItem.CartItemUnits
 };

 OrderItemInfoProvider.SetOrderItemInfo(newItem);

 newOrder.OrderTotalPrice = cartItem.UnitPrice * cartItem.CartItemUnits;
 OrderInfoProvider.SetOrderInfo(newOrder);

I realize it's not best practice at the moment but I just want to get one item working and then I can refine it. I've also tried to only include the most relevant parts, hence I haven't posted the customer or status parts but they seem to work fine. I would be really grateful if someone could point out what I'm missing or doing wrong, or if there is a better method of checking out a cart once an item has been added to it.

Thanks in advance for any help :)

Correct Answer

Suneel Jhangiani answered on June 2, 2017 13:24

Yes, the difference is that OrderTotal price holds the the order value in the currency the order was made in if it was done on a multi-currency system (it holds null if the order was in the main currency). The OrderTotalPriceInMainCurrency always holds a value which for multi-currency systems should be the OrderTotalPrice converted by the OrderExchangeRate.

Due to the complexity of the Order handling I really do recommend you stick to using the ShoppingCart to work with Orders. Kentico also uses this on its admin pages.

In addition Kentico v11 will bring a new calculation model and by using the ShoppingCart you should be able to update your custom code easily. Another big change is that Kentico v11 will use decimal's instead of double's.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Suneel Jhangiani answered on June 1, 2017 14:23 (last edited on June 1, 2017 14:25)

I would recommend using the ShoppingCartinfoProvider.SetOrder(ShoppingCartInfo cart) method to save your order since this should recalculate the data.

You can ensure the it recalculates the totals by calling the ShoppingCartInfo.InvalidateCalculations() method.

1 votesVote for this answer Mark as a Correct answer

Charlie Bodnar-Horvath answered on June 2, 2017 09:25 (last edited on June 2, 2017 09:29)

Hi Suneel,

Thanks for your response. I've started using SetOrder as you've suggested but the problem was actually that I was setting newOrder.OrderTotalPrice instead of newOrder.OrderTotalPriceInMainCurrency and changing that solved the issue.

0 votesVote for this answer Mark as a Correct answer

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