Basket Checkout thru code
Posted: Thu Apr 09, 2009 2:08 pm
I'm trying to create an Order object in code. I'm doing this by populating a Basket and running the Basket.Checkout() method.
Populating the Basket with BasketItems and performing the Checkout method is working, but I'm having 1 problem. All my Totals (i.e., Item Sub Total, Discount Amounts, etc..) are correct except for Shipping. Shipping is empty and not shipping item is added to the Order.Items collection. I'm using an integrated carrier (UPS), but also a flat rate ship method. When stepping through the code, the Basket.Shipment[x].ShipMethodId is "0" even though I set it to a valid ShipMethodId (1). I'm guessing that is the problem, but not sure why the ShipMethodId is being cleared out.
Here is a copy of the code.
Any ideas why ShipMethodId would be coming up empty???
Populating the Basket with BasketItems and performing the Checkout method is working, but I'm having 1 problem. All my Totals (i.e., Item Sub Total, Discount Amounts, etc..) are correct except for Shipping. Shipping is empty and not shipping item is added to the Order.Items collection. I'm using an integrated carrier (UPS), but also a flat rate ship method. When stepping through the code, the Basket.Shipment[x].ShipMethodId is "0" even though I set it to a valid ShipMethodId (1). I'm guessing that is the problem, but not sure why the ShipMethodId is being cleared out.
Here is a copy of the code.
Code: Select all
Order order = null;
try
{
//initialize user
User user = UserDataSource.CreateUserInstance();
user.IsAnonymous = true;
user.Save();
// create basket
Basket basket = user.Basket;
//setup new address
Address address = new Address();
address.Address1 = "1111 15th Street";
address.City = "Austin";
address.Province = "TX";
address.PostalCode = "78704";
address.LastName = "Doe";
address.FirstName = "John";
address.Email = "John@Doe.com";
address.Phone = "(555) 123-1234";
address.CountryCode = "US";
address.UserId = user.UserId;
address.Save();
// Save anonymous user
user.PrimaryAddressId = address.AddressId;
user.Save();
//Create Basket Items
ProductCollection productList;
ProductVariantCollection variantList;
foreach (CustomItem item in CustomItems)
{
BasketItem bi = null;
// Check to see if this item is a product
productList = ProductDataSource.LoadForCriteria(string.Format("SKU = '{0}'", item.ProductSKU));
if (productList != null && productList.Count > 0)
{
bi = BasketItemDataSource.CreateForProduct(productList[0].ProductId, (short)item.QtyOrdered);
}
if (bi == null) // If bi is null, then the item was not found as a product
{
// Check to see if this item is a product variant
variantList = ProductVariantDataSource.LoadForCriteria(string.Format("SKU = '{0}'", item.ProductSKU));
if (variantList != null && variantList.Count > 0)
{
bi = BasketItemDataSource.CreateForProduct(variantList[0].ProductId, (short)item.QtyOrdered, variantList[0].OptionList);
}
}
if (bi == null)
{
throw new Exception(string.Format("Product not found: {0}",item.Name));
}
basket.Items.Add(bi);
}
basket.Save();
//add a shipments to the order
BasketShipment shipment = new BasketShipment();
// Set Address information
Address shipAddress = new Address();
shipAddress.Address1 = "1111 15th Street";
shipAddress.City = "Austin";
shipAddress.CountryCode = "US";
shipAddress.Email = "john@doe.com";
shipAddress.FirstName = "John";
shipAddress.LastName = "Doe";
shipAddress.Phone = "(555) 123-1234";
shipAddress.PostalCode = "78704";
shipAddress.Province = "TX";
shipAddress.Residence = "tue;
shipAddress.UserId = user.UserId;
shipAddress.Save();
shipment.AddressId = shipAddress.AddressId;
shipment.BasketId = basket.BasketId;
// Set Ship Method
shipment.ShipMethodId = 1;
// Set reminding items
shipment.ShipMessage = "Fragile (it's French for major award)";
shipment.WarehouseId = 1;
shipment.Save();
basket.Shipments.Add(shipment);
basket.Save();
// Add Coupons
foreach (string couponCode in CouponCodes)
{
string validateCouponMessage = ValidateCoupon(couponCode, basket);
if (validateCouponMessage != "Success")
{
throw new Exception(validateCouponMessage);
}
}
basket.Save();
//PROCESS THE CHECKOUT
basket.Package();
basket.Recalculate();
Payment payment = new Payment();
payment.Amount = basket.Items.TotalPrice();
CheckoutRequest checkoutRequest = new CheckoutRequest(payment);
CheckoutResponse checkoutResponse = basket.Checkout(checkoutRequest);
//if successful
if (checkoutResponse.Success)
{
order = OrderDataSource.Load(checkoutResponse.OrderId);
order.OrderDate = System.Convert.ToDateTime(NewOrderDate);
order.Save();
}
}
catch (Exception ex) {
throw ex;
}
return order;