Orders
List of examples:
Orders
Creating a new order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
// Gets a status for the order
OrderStatusInfo orderStatus = OrderStatusInfo.Provider.Get("NewStatus", SiteContext.CurrentSiteID);
// Gets a currency for the order
CurrencyInfo currency = CurrencyInfo.Provider.Get("NewCurrency", SiteContext.CurrentSiteID);
// Gets the customer's address
AddressInfo customerAddress = AddressInfo.Provider.Get()
.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
OrderInfo.Provider.Set(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
OrderAddressInfo.Provider.Set(orderBillingAddress);
OrderAddressInfo.Provider.Set(orderShippingAddress);
}
Updating an order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderInfo.Provider.Set(order);
}
}
Updating multiple orders
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's orders
var orders = OrderInfo.Provider.Get().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
OrderInfo.Provider.Set(order);
}
}
Deleting an order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's order
OrderInfo order = OrderInfo.Provider.Get()
.WhereEquals("OrderCustomerID", customer.CustomerID)
.TopN(1)
.FirstOrDefault();
if (order != null)
{
// Deletes the order
OrderInfo.Provider.Delete(order);
}
}
Order items
Creating an order item
// Gets the 'NewProduct' product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.TopN(1)
.FirstOrDefault();
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderItemInfo.Provider.Set(newItem);
}
}
Updating an order item
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderItemInfo.Provider.Set(orderItem);
}
}
}
Updating multiple order items
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderItemInfo.Provider.Set(orderItem);
}
}
}
Deleting an order item
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderItemInfo.Provider.Delete(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
OrderStatusInfo.Provider.Set(newStatus);
Updating an order status
// Gets the order status
OrderStatusInfo updateStatus = OrderStatusInfo.Provider.Get("NewStatus", SiteContext.CurrentSiteID);
if (updateStatus != null)
{
// Updates the display name of the order status
updateStatus.StatusDisplayName = updateStatus.StatusDisplayName.ToLowerCSafe();
// Saves the changes to the database
OrderStatusInfo.Provider.Set(updateStatus);
}
Updating multiple order statuses
// Gets all order statuses on the current site whose code name starts with 'NewStatus'
var statuses = OrderStatusInfo.Provider.Get()
.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
OrderStatusInfo.Provider.Set(modifyStatus);
}
Deleting an order status
// Gets the order status
OrderStatusInfo deleteStatus = OrderStatusInfo.Provider.Get("NewStatus", SiteContext.CurrentSiteID);
if (deleteStatus != null)
{
// Deletes the order status
OrderStatusInfo.Provider.Delete(deleteStatus);
}
Changing an order’s status to the next enabled order status
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderStatusUserInfo.Provider.Set(orderStatusChange);
// Assigns the new order status to the order
order.OrderStatusID = nextOrderStatus.StatusID;
// Saves the updated order to the database
OrderInfo.Provider.Set(order);
}
}
}
Deleting the status history of an order
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
.WhereEquals("CustomerLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (customer != null)
{
// Gets the customer's first order
OrderInfo order = OrderInfo.Provider.Get()
.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
OrderStatusUserInfo.Provider.Delete(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);
}