Skip to content

Commerce Order API Reference

advanced
📜Corecommerce

The Order API manages purchase orders after checkout. IOrderRepository loads and saves orders. IOrderSearchService provides filtered queries. OrderStatusManager handles state transitions.

Optimizely.Commerce.Order

InterfacePurpose
IOrderRepositoryLoad, save, delete orders
IOrderSearchServiceSearch and filter orders
OrderStatusManagerManage order state transitions

Loads an order by its tracking number.

Parameters

NameTypeDescription
orderGroupIdintOrder group ID (tracking number)

Returns: T where T is IPurchaseOrder, or null

Loads orders for a customer.

Parameters

NameTypeDescription
customerIdGuidCustomer contact GUID
namestringOrder name filter (pass null for all)

Returns: IEnumerable<T>

Load orders for a customer
csharp
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);
  }
}

Searches orders with filtering and pagination.

Parameters

NameTypeDescription
filterOrderSearchFilterSearch criteria

Returns: IEnumerable<IPurchaseOrder>

PropertyTypeDescription
StartingRecordintZero-based offset for pagination
RecordsToRetrieveintPage size
OrderStatusIdOrderStatus?Filter by status
ModifiedFromDateTime?Orders modified after this date
ModifiedToDateTime?Orders modified before this date
CustomerIdGuid?Filter by customer
MarketIdMarketId?Filter by market
Search orders by status
csharp
public IEnumerable<IPurchaseOrder> GetPendingOrders(int pageSize, int page)
{
  var filter = new OrderSearchFilter
  {
      StartingRecord = page * pageSize,
      RecordsToRetrieve = pageSize,
      OrderStatusId = OrderStatus.InProgress
  };

  return _orderSearchService.FindPurchaseOrders(filter);
}
OrderStatusInt valueDescription
InProgress3Default state after checkout
OnHold6Paused for review
Completed10Fulfilled and closed
Cancelled20Cancelled by admin or customer
PartiallyShipped15Some shipments shipped
AwaitingExchange21Pending return exchange

Valid transitions enforced by OrderStatusManager:

InProgress → OnHold → InProgress
InProgress → PartiallyShipped → Completed
InProgress → Completed
InProgress → Cancelled
OnHold → Cancelled
PartiallyShipped → Completed
Update order status
csharp
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);
}
PropertyTypeDescription
OrderNumberstringHuman-readable order number
OrderLinkOrderReferenceUnique order reference
StatusstringCurrent status string
CreatedDateTimeDate the order was placed
ModifiedDateTimeLast modification date
CurrencyCurrencyOrder currency
MarketIdMarketIdMarket the order belongs to
GetFirstForm()IOrderFormPrimary order form with line items
GetFirstShipment()IShipmentPrimary shipment
ExceptionCause
OrderNotFoundExceptionOrder ID does not exist
InvalidStateTransitionExceptionStatus change not allowed from current state
AccessDeniedExceptionUser lacks permission to modify the order