public bool Delete(object OrderItemId) { return(OrderItem.Delete(OrderItemId) == 1); }
/// <summary> /// Migrates the cart. /// </summary> /// <param name="anonymousCartId">The anonymous cart id.</param> /// <param name="registeredCartId">The registered cart id.</param> public static void MigrateCart(string anonymousCartId, string registeredCartId) { Order anonymousOrder = new OrderController().FetchOrder(anonymousCartId); Order registeredOrder = new OrderController().FetchOrder(registeredCartId); //first see if there is an order for the now-recognized user if (string.IsNullOrEmpty(registeredOrder.OrderNumber) && !string.IsNullOrEmpty(anonymousOrder.OrderNumber)) { //if not, just update the old order with the new user's username anonymousOrder.UserName = registeredCartId; anonymousOrder.Save(registeredCartId); } else { //the logged-in user has an existing basket. //if there is no basket from their anon session, //we don't need to do anything if (!string.IsNullOrEmpty(anonymousOrder.OrderNumber)) { //in this case, there is an order (cart) from their anon session //and an order that they had open from their last session //need to marry the items. //this part is up to your business needs - some merchants //will want to replace the cart contents //others will want to synch them. We're going to assume that //this scenario will synch the existing items. //############### Synch the Cart Items if (registeredOrder.OrderItemCollection.Count > 0 && anonymousOrder.OrderItemCollection.Count > 0) { //there are items in both carts, move the old to the new //when synching, find matching items in both carts //update the quantities of the matching items in the logged-in cart //removing them from the anon cart //a switch to tell us if we need to update the from orders //1.) Find items that are the same between the two carts and add the anon quantity to the registered user quantity //2.) Mark found items as items to be removed from the anon cart. ArrayList toBeRemoved = new ArrayList(anonymousOrder.OrderItemCollection.Count); for (int i = 0; i < anonymousOrder.OrderItemCollection.Count; i++) { OrderItem foundItem = registeredOrder.OrderItemCollection.Find(delegate(OrderItem orderItemToFind) { return((orderItemToFind.Sku == anonymousOrder.OrderItemCollection[i].Sku) && (orderItemToFind.Attributes == anonymousOrder.OrderItemCollection[i].Attributes)); }); if (foundItem != null) { foundItem.Quantity += anonymousOrder.OrderItemCollection[i].Quantity; toBeRemoved.Add(i); } } //3.) Now remove any foundItems from the anon cart, but trim it up first toBeRemoved.TrimToSize(); for (int i = 0; i < toBeRemoved.Count; i++) { anonymousOrder.OrderItemCollection.RemoveAt((int)toBeRemoved[i]); } //4.) Move over to the registered user cart any remaining items in the anon cart. foreach (OrderItem anonItem in anonymousOrder.OrderItemCollection) { //reset the orderID anonItem.OrderId = registeredOrder.OrderId; registeredOrder.OrderItemCollection.Add(anonItem); } //5.) Finally, save it down to the DB // (Since we know toOrder.Items.Count > 0 && fromOrder.Items.Count > 0, we know a Save needs to occur) registeredOrder.OrderItemCollection.SaveAll(registeredCartId); registeredOrder.Save(registeredCartId); } else if (registeredOrder.OrderItemCollection.Count == 0) { //items exist only in the anon cart //move the anon items to the new cart //then save the order and the order items. registeredOrder.IsNew = true; registeredOrder.OrderStatusDescriptorId = anonymousOrder.OrderStatusDescriptorId; registeredOrder.OrderGuid = anonymousOrder.OrderGuid; registeredOrder.UserName = registeredCartId; registeredOrder.OrderNumber = anonymousOrder.OrderNumber; registeredOrder.Save(registeredCartId); foreach (OrderItem item in anonymousOrder.OrderItemCollection) { //reset the orderID on each item item.OrderId = registeredOrder.OrderId; registeredOrder.OrderItemCollection.Add(item); } registeredOrder.OrderItemCollection.SaveAll(registeredCartId); registeredOrder.Save(registeredCartId); } else if (anonymousOrder.OrderItemCollection.Count == 0) { //no items in the old cart, do nothing } //finally, drop the anon order from the DB, we don't want to //keep it OrderItem.Delete(OrderItem.Columns.OrderId, anonymousOrder.OrderId); Order.Delete(anonymousOrder.OrderId); } } }