Orders
List of examples:
Orders
Creating a new order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
// Gets a status for the order
OrderStatusInfo orderStatus = OrderStatusInfoProvider.GetOrderStatusInfo("NewStatus", SiteContext.CurrentSiteName);
// Gets a currency for the order
CurrencyInfo currency = CurrencyInfoProvider.GetCurrencyInfo("NewCurrency", SiteContext.CurrentSiteName);
// Gets the customer's address
AddressInfo customerAddress = AddressInfoProvider.GetAddresses()
.WhereEquals("AddressCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if ((customer != null) && (orderStatus != null) && (currency != null) && (customerAddress != null))
{
// Creates a new order object and sets its properties
OrderInfo newOrder = new OrderInfo
{
OrderInvoiceNumber = "1",
OrderTotalPrice = 200,
OrderGrandTotal = 200,
OrderTotalTax = 30,
OrderDate = DateTime.Now,
OrderStatusID = orderStatus.StatusID,
OrderCustomerID = customer.CustomerID,
OrderSiteID = SiteContext.CurrentSiteID,
OrderCurrencyID = currency.CurrencyID
};
// Saves the order to the database
OrderInfoProvider.SetOrderInfo(newOrder);
var addressConverter = Service.Resolve<IAddressConverter>();
// Prepares the order addresses from the customer's address
OrderAddressInfo orderBillingAddress = addressConverter.Convert(customerAddress, newOrder.OrderID, AddressType.Billing);
OrderAddressInfo orderShippingAddress = addressConverter.Convert(customerAddress, newOrder.OrderID, AddressType.Shipping);
// Sets the order addresses
OrderAddressInfoProvider.SetAddressInfo(orderBillingAddress);
OrderAddressInfoProvider.SetAddressInfo(orderShippingAddress);
}
Updating an order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Updates the price of the order
order.OrderTotalPrice = order.OrderTotalPrice + 200;
order.OrderGrandTotal = order.OrderGrandTotal + 200;
// Saves the changes to the database
OrderInfoProvider.SetOrderInfo(order);
}
}
Updating multiple orders
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's orders
var orders = OrderInfoProvider.GetOrders().WhereEquals("OrderCustomerID", customer.CustomerID);
// Loops through the customer's orders
foreach (OrderInfo order in orders)
{
// Updates the price of the order
order.OrderTotalPrice = order.OrderTotalPrice + 200;
order.OrderGrandTotal = order.OrderGrandTotal + 200;
// Saves the changes to the database
OrderInfoProvider.SetOrderInfo(order);
}
}
Deleting an order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Deletes the order
OrderInfoProvider.DeleteOrderInfo(order);
}
}
Order items
Creating an order item
// Gets the 'NewProduct' product
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereEquals("SKUName", "NewProduct")
.TopN(1)
.FirstOrDefault();
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if ((order != null) && (product != null))
{
// Creates a new order item object and sets its properties
OrderItemInfo newItem = new OrderItemInfo
{
OrderItemSKUName = "NewProduct",
OrderItemOrderID = order.OrderID,
OrderItemSKUID = product.SKUID,
OrderItemUnitPrice = 200,
OrderItemUnitCount = 1
};
// Saves the order item object to the database
OrderItemInfoProvider.SetOrderItemInfo(newItem);
}
}
Updating an order item
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Gets the first item in the order
OrderItemInfo orderItem = OrderItemInfoProvider.GetOrderItems(order.OrderID).FirstOrDefault();
if (orderItem != null)
{
// Updates the order item's SKU name
orderItem.OrderItemSKUName = orderItem.OrderItemSKUName.ToLowerCSafe();
// Saves the changes to the database
OrderItemInfoProvider.SetOrderItemInfo(orderItem);
}
}
}
Updating multiple order items
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Gets all items in the given order
var orderItems = OrderItemInfoProvider.GetOrderItems(order.OrderID);
// Loops through the order items
foreach (OrderItemInfo orderItem in orderItems)
{
// Updates the order item's SKU name
orderItem.OrderItemSKUName = orderItem.OrderItemSKUName.ToUpperCSafe();
// Saves the order item to the database
OrderItemInfoProvider.SetOrderItemInfo(orderItem);
}
}
}
Deleting an order item
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Gets the first item from the order
OrderItemInfo orderItem = OrderItemInfoProvider.GetOrderItems(order.OrderID).FirstOrDefault();
if (orderItem != null)
{
// Deletes the order item
OrderItemInfoProvider.DeleteOrderItemInfo(orderItem);
}
}
}
Order statuses
Creating an order status
// Creates a new order status object
OrderStatusInfo newStatus = new OrderStatusInfo();
// Sets the order status properties
newStatus.StatusDisplayName = "New status";
newStatus.StatusName = "NewStatus";
newStatus.StatusEnabled = true;
newStatus.StatusSiteID = SiteContext.CurrentSiteID;
newStatus.StatusOrder = 1;
// Saves the order status to the database
OrderStatusInfoProvider.SetOrderStatusInfo(newStatus);
Updating an order status
// Gets the order status
OrderStatusInfo updateStatus = OrderStatusInfoProvider.GetOrderStatusInfo("NewStatus", SiteContext.CurrentSiteName);
if (updateStatus != null)
{
// Updates the display name of the order status
updateStatus.StatusDisplayName = updateStatus.StatusDisplayName.ToLowerCSafe();
// Saves the changes to the database
OrderStatusInfoProvider.SetOrderStatusInfo(updateStatus);
}
Updating multiple order statuses
// Gets all order statuses on the current site whose code name starts with 'NewStatus'
var statuses = OrderStatusInfoProvider.GetOrderStatuses()
.OnSite(SiteContext.CurrentSiteID)
.WhereStartsWith("StatusName", "NewStatus");
// Loops through the order statuses
foreach (OrderStatusInfo modifyStatus in statuses)
{
// Updates the display name of the order status
modifyStatus.StatusDisplayName = modifyStatus.StatusDisplayName.ToUpperCSafe();
// Saves the changes to the database
OrderStatusInfoProvider.SetOrderStatusInfo(modifyStatus);
}
Deleting an order status
// Gets the order status
OrderStatusInfo deleteStatus = OrderStatusInfoProvider.GetOrderStatusInfo("NewStatus", SiteContext.CurrentSiteName);
if (deleteStatus != null)
{
// Deletes the order status
OrderStatusInfoProvider.DeleteOrderStatusInfo(deleteStatus);
}
Changing an order’s status to the next enabled order status
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Gets the next enabled status for the order
OrderStatusInfo nextOrderStatus = OrderStatusInfoProvider.GetNextEnabledStatus(order.OrderStatusID);
if (nextOrderStatus != null)
{
// Creates a new object representing an order status change (performed by a specific user)
OrderStatusUserInfo orderStatusChange = new OrderStatusUserInfo()
{
OrderID = order.OrderID,
ChangedByUserID = MembershipContext.AuthenticatedUser.UserID,
FromStatusID = order.OrderStatusID,
ToStatusID = nextOrderStatus.StatusID,
Date = DateTime.Now
};
// Saves the status change to the order's history
OrderStatusUserInfoProvider.SetOrderStatusUserInfo(orderStatusChange);
// Assigns the new order status to the order
order.OrderStatusID = nextOrderStatus.StatusID;
// Saves the updated order to the database
OrderInfoProvider.SetOrderInfo(order);
}
}
}
Deleting the status history of an order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfoProvider.GetOrders()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Gets the order status changes in the order's history
var statuses = OrderStatusUserInfoProvider.GetOrderStatusHistory(order.OrderID);
// Loops through the order status changes
foreach (OrderStatusUserInfo status in statuses)
{
// Deletes the order status changes
OrderStatusUserInfoProvider.DeleteOrderStatusUserInfo(status);
}
}
}
Invoices
Updating an invoice
// Gets the current site name
string siteName = SiteContext.CurrentSiteName;
// Gets the current site's invoice template
string invoice = ECommerceSettings.InvoiceTemplate(siteName);
if (!String.IsNullOrEmpty(invoice))
{
// Prepares a new string with the invoice text
invoice = "<h1>Updated invoice</h1>" + invoice;
// Saves the invoice changes to the database
SettingsKeyInfoProvider.SetValue(ECommerceSettings.INVOICE_TEMPLATE, siteName, invoice);
}