Loading...
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Order manipulation

When manipulating orders in Episerver commerce, please refer to the namespace EPiServer.Commerce.Order, specfically the classes EPiServer.Commerce.Order.IOrderRepositoryEPiServer.Commerce.Order.IOrderRepositoryExtensions, EPiServer.Commerce.Order.IOrderGroupExtensions, and EPiServer.Commerce.Order.IOrderFactory

Creating orders

When creating orders using the default implementation of IOrderFactory, there is always an IOrderForm in the Forms collection. There is also always an IShipment on the Shipments collection of the IForm. If you create a custom IOrderFactory, assume you need this as well.

var orderFactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();

//Create cart
var cart = orderFatory.CreateCart(contactId, "Default");

//Create purchase order
var purchaseOrder = orderFactory.CreatePurchaseOrder(contactId);

//Create payment plan
var paymentPlan = orderFactory.CreatePaymentPlan(contactId, "Default");

Loading orders

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();

//loading cart will return null if does not exist
var cart = orderRepository.LoadCart(contactId, "Default");
var cart = orderRepository.Load<ICart>(contactId, "Default").FirstOrDefault();
var cart = orderRepository.Load<ICart>(orderGroupId).FirstOrDefault();
var cart = orderRepository.Load(orderReference) as ICart;

//load or create cart will create cart if does not exist
var cart = orderRepository.LoadOrCreateCart(contactId, "Default");

//loading wishlist will return null if does not exist
var wishlist = orderRepository.LoadCart(contactId, "Wishlist");
var wishlist = orderRepository.Load<ICart>(contactId, "Wishlist").FirstOrDefault();
var wishlist = orderRepository.Load<ICart>(orderGroupId).FirstOrDefault();
var wishlist = orderRepository.Load(orderReference) as ICart;

//load or create wishlist will create cart if does not exist
var wishlist = orderRepository.LoadOrCreateCart(contactId, "Wishlist");

//loading purchase order
var purchaseOrder = orderRepository.Load(orderReference) as IPurchaseOrder;
var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderGroupId);

//loading purchase orders for customer
var purchaseOrders = orderRepository.Load<IPurchaseOrder>(contactId, "Default");

//loading payment plan
var paymentPlan= orderRepository.Load(orderReference) as IPaymentPlan;
var paymentPlan = orderRepository.Load<IPaymentPlan>(orderGroupId);

//loading payment plans for customer
var paymentPlans = orderRepository.Load<IPaymentPlan>(contactId, "Default")

//loading all order for customer
var orders = orderRepository.Load(contactId, "Default")

Saving orders

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();

var reference = orderRepository.Save(cart);
var reference = orderRepository.Save(purchaseOrder);
var reference = orderRepository.Save(paymentPlan);
var reference = orderRepository.SaveAsPurchaseOrder(cart);
var reference = orderRepository.SaveAsPaymentPlan(cart);

Deleting orders

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();

orderRespository.Delete(orderRefernce);

Working with order forms

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderfactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart(contactid, "Default");


//create and add new order form (b2b)
var orderForm = orderFactory.CreateOrderForm();
cart.Forms.Add(orderForm);
orderForm.Name = "Default";

//delete order form
cart.Forms.Remove(orderForm);

Working with shipments

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderfactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart(contactId, "Default");

//Create and add shipment to first form
var shipment = orderFactory.CreateShipment();
//pass in orderFactory for unit testing as a from will be created if there is none on the cart.
cart.AddShipment(shipment, orderFactory);
//Set address after adding to collection becasue of limitation in implementation
shipment.ShippingAddress = orderFactory.CreateOrderAddress();

//Create and add shipment to second form (b2b)
var secondForm = cart.Forms.Last();
var shipment = orderFactory.CreateShipment();
cart.AddShipment(secondForm, shipment);
//Set address after adding to collection becasue of limitation in implementation
shipment.ShippingAddress = orderFactory.CreateOrderAddress();

//Remove shipment from first form
cart.GetFirstForm().Shipments.Remove(shipment);

//Remove shipment from second form (b2b)
cart.Forms[1].Shipments.Remove(shipment);

Working with payments

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderfactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart(contactId, "Default");

//Create and add payment to first form
var creditCard = orderFactory.CreateCardPayment();
var invoice = orderFactory.CreatePayment();
//pass in orderFactory for unit testing as a from will be created if there is none on the cart.
cart.AddPayment(creditCard, orderFactory);
cart.AddPayment(invoice, orderFactory);
//Set address after adding to collection becasue of limitation in implementation
creditCard.BillingAddress = orderFactory.CreateOrderAddress();

//Create and add payment to second form (b2b)
var secondForm = cart.Forms.Last();
var creditCard = orderFactory.CreateCardPayment();
var invoice = orderFactory.CreatePayment();
cart.AddPayment(secondForm, creditCard);
cart.AddPayment(secondForm, invoice);
//Set address after adding to collection becasue of limitation in implementation
creditCard.BillingAddress = orderFactory.CreateOrderAddress();

//Remove payment from first form
cart.GetFirstForm().Payments.Remove(payment);

//Remove shipment from second form (b2b)
cart.Forms[1].Payments.Remove(payment);

Working with line items

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderfactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart(contactId, "Default");

//add line item to first shipment on first form
var lineItem = orderFactory.CreateLineItem("code");

//use orderFactory for unit testing
cart.AddLineItem(lineItem, orderFactory);

//add line item to second shipment on first form
var shipment = cart.GetFirstForm().Shipments.Last();
var lineItem = orderFactory.CreateLineItem("code"); 
cart.AddLineItem(shipment, lineItem); 

//add line item to second form first shipment
var orderForm = cart.Forms.Last();
var lineItem = orderFactory.CreateLineItem("code");

//add orderFactory for unit testing 
cart.AddLineItem(orderForm, lineItem, orderFactory);

//remove line item from first form first shipment 
cart.GetFirstShipment().Remove(lineItem);

//remove line item from first form second shipment
cart.GetFirstForm().Shipments.Last().Remove(lineItem);

//remove line item from second form first shipment (b2b)
cart.Forms[1].Shipments.First().Remove(lineItem)

Working with addresses

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderFactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart(contactId, "Default");

var address = orderFactory.CreateAddress();

//Use Id to reuse
address.Id = "Billing";
cart.GetFirstForm().Payments.First().BillingAddress = address;

//Since there is already an address with "Billing" it will use that address instead of creating another one on the order.
var reuseOtherAddress = orderFactory.CreateAddress();
reuseOtherAddress.Id = "Billing";
cart.GetFirstShipment().ShippingAddress = reuseOtherAddress;

 
//Region Name and Region Code should be used when dealing with states
address.RegionName = "California";
address.RegionCode = "CA";
address.CountryCode = "US";
address.Country = "United States";

Working with notes

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderFactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart(contactId, "Default");

var notes = cart.Notes;

var note = orderFactory.CreateOrderNote();
note.CustomerId = contactId;
note.Type = OrderNoteTypes.Custom.ToString();
note.Title = "Noted";
note.Detail = "Something should be noted.";
note.Created = DateTime.UtcNow;
notes.Add(note);

orderRepository.Save(cart);

Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 12, 2015

Recommended reading