Overview
Order Lifecycle exposes four services - OrderLifecycleLogger for reading and writing log entries, StatsService for store-wide metrics, AiInsightsService for Claude prompt building and generation, and ExportService for CSV export - plus a Twig variable for template-level access. See the Services reference for the full method list on each.
Accessing the Service
use johnhenry\orderlifecycle\OrderLifecycle;
$logger = OrderLifecycle::getInstance()->getLogger();
$stats = OrderLifecycle::getInstance()->getStats();
$ai = OrderLifecycle::getInstance()->getAiInsights();
$export = OrderLifecycle::getInstance()->getExport();Twig Variable
The plugin registers a craft.orderLifecycle Twig variable:
{# Log checkout started (idempotent - safe to call on every page load) #}
{% do craft.orderLifecycle.logCheckoutStarted(cart) %}
{# Get the logger service from Twig #}
{% set logger = craft.orderLifecycle.getLogger() %}
{# Get the AI insights service, e.g. to detect whether a saved insight is
structured JSON or narrative markdown before rendering it #}
{% set structured = craft.orderLifecycle.aiInsights.decodeStructuredInsights(savedInsights.message) %}Basic Usage
Logging a Custom Event
use johnhenry\orderlifecycle\OrderLifecycle;
use johnhenry\orderlifecycle\enums\EventType;
OrderLifecycle::getInstance()->getLogger()->log(
$order,
EventType::STATUS_CHANGED,
['note' => 'Escalated by support'],
'Order manually escalated'
);Retrieving Logs for an Order
use johnhenry\orderlifecycle\OrderLifecycle;
$logs = OrderLifecycle::getInstance()->getLogger()->getLogsForOrder($order->id);
foreach ($logs as $log) {
echo $log['type'] . ': ' . $log['message'] . PHP_EOL;
}Checking the Last Snapshot
use johnhenry\orderlifecycle\OrderLifecycle;
$snapshot = OrderLifecycle::getInstance()->getLogger()->getLastSnapshot($order->id);
if ($snapshot) {
$previousTotal = $snapshot['order']['totalPrice'];
}Event Types
All event types are defined as cases on the EventType enum:
use johnhenry\orderlifecycle\enums\EventType;
EventType::CART_CREATED
EventType::LINE_ITEM_ADDED
EventType::ORDER_COMPLETED
EventType::PAYMENT_REFUNDED
// ... see Events reference for the full listNext Steps
- Services - Full method reference for all four services
- Events - All
EventTypeenum cases and what triggers them - Event Tracking Guide - Practical usage examples