Commerce Order API Reference
Corecommerce
Overview
Section titled “Overview”The Order API manages purchase orders after checkout. IOrderRepository loads and saves orders. IOrderSearchService provides filtered queries. OrderStatusManager handles state transitions.
Namespace
Section titled “Namespace”Optimizely.Commerce.Order
Key interfaces
Section titled “Key interfaces”| Interface | Purpose |
|---|---|
IOrderRepository | Load, save, delete orders |
IOrderSearchService | Search and filter orders |
OrderStatusManager | Manage order state transitions |
IOrderRepository — Order methods
Section titled “IOrderRepository — Order methods”Load<T>(int)
Section titled “Load<T>(int)”Loads an order by its tracking number.
Parameters
| Name | Type | Description |
|---|---|---|
orderGroupId | int | Order group ID (tracking number) |
Returns: T where T is IPurchaseOrder, or null
Load<T>(Guid, string)
Section titled “Load<T>(Guid, string)”Loads orders for a customer.
Parameters
| Name | Type | Description |
|---|---|---|
customerId | Guid | Customer contact GUID |
name | string | Order name filter (pass null for all) |
Returns: IEnumerable<T>
Load orders for a customer
public class OrderService
{
private readonly IOrderRepository _orderRepository;
public OrderService(IOrderRepository orderRepository)
{
_orderRepository = orderRepository;
}
public IEnumerable<IPurchaseOrder> GetCustomerOrders(Guid customerId)
{
return _orderRepository.Load<IPurchaseOrder>(customerId, null);
}
public IPurchaseOrder GetOrder(int orderId)
{
return _orderRepository.Load<IPurchaseOrder>(orderId);
}
} IOrderSearchService
Section titled “IOrderSearchService”FindPurchaseOrders(OrderSearchFilter)
Section titled “FindPurchaseOrders(OrderSearchFilter)”Searches orders with filtering and pagination.
Parameters
| Name | Type | Description |
|---|---|---|
filter | OrderSearchFilter | Search criteria |
Returns: IEnumerable<IPurchaseOrder>
OrderSearchFilter properties
Section titled “OrderSearchFilter properties”| Property | Type | Description |
|---|---|---|
StartingRecord | int | Zero-based offset for pagination |
RecordsToRetrieve | int | Page size |
OrderStatusId | OrderStatus? | Filter by status |
ModifiedFrom | DateTime? | Orders modified after this date |
ModifiedTo | DateTime? | Orders modified before this date |
CustomerId | Guid? | Filter by customer |
MarketId | MarketId? | Filter by market |
Search orders by status
public IEnumerable<IPurchaseOrder> GetPendingOrders(int pageSize, int page)
{
var filter = new OrderSearchFilter
{
StartingRecord = page * pageSize,
RecordsToRetrieve = pageSize,
OrderStatusId = OrderStatus.InProgress
};
return _orderSearchService.FindPurchaseOrders(filter);
} Order states
Section titled “Order states”| OrderStatus | Int value | Description |
|---|---|---|
InProgress | 3 | Default state after checkout |
OnHold | 6 | Paused for review |
Completed | 10 | Fulfilled and closed |
Cancelled | 20 | Cancelled by admin or customer |
PartiallyShipped | 15 | Some shipments shipped |
AwaitingExchange | 21 | Pending return exchange |
State transitions
Section titled “State transitions”Valid transitions enforced by OrderStatusManager:
InProgress → OnHold → InProgressInProgress → PartiallyShipped → CompletedInProgress → CompletedInProgress → CancelledOnHold → CancelledPartiallyShipped → CompletedUpdate order status
public void CompleteOrder(IPurchaseOrder order)
{
OrderStatusManager.SetOrderStatus(
order,
OrderStatus.Completed);
_orderRepository.Save(order);
}
public void CancelOrder(IPurchaseOrder order)
{
OrderStatusManager.SetOrderStatus(
order,
OrderStatus.Cancelled);
_orderRepository.Save(order);
} IPurchaseOrder properties
Section titled “IPurchaseOrder properties”| Property | Type | Description |
|---|---|---|
OrderNumber | string | Human-readable order number |
OrderLink | OrderReference | Unique order reference |
Status | string | Current status string |
Created | DateTime | Date the order was placed |
Modified | DateTime | Last modification date |
Currency | Currency | Order currency |
MarketId | MarketId | Market the order belongs to |
GetFirstForm() | IOrderForm | Primary order form with line items |
GetFirstShipment() | IShipment | Primary shipment |
Common exceptions
Section titled “Common exceptions”| Exception | Cause |
|---|---|
OrderNotFoundException | Order ID does not exist |
InvalidStateTransitionException | Status change not allowed from current state |
AccessDeniedException | User lacks permission to modify the order |