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 :)