Customer Lifetime Value Snapshot
Calculates customer lifetime value metrics from paid, refunded, and pending order records.
Purpose
Calculates customer lifetime value metrics from paid, refunded, and pending order records.
Snippet details
ContextEcommerceLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Customer Lifetime Value Snapshot.
*
* Purpose:
* Calculates customer lifetime value metrics from paid, refunded, and pending order records.
*
* @param array $orders Customer order rows.
* @return array Lifetime value snapshot.
*/
function ogSnippetCustomerLifetimeValueSnapshot(array $orders): array {
$paid_cents = 0;
$refunded_cents = 0;
$pending_cents = 0;
foreach ($orders as $order) {
if ($order['status'] === 'paid') {
$paid_cents += (int) $order['total_cents'];
}
if ($order['status'] === 'refunded') {
$refunded_cents += (int) $order['total_cents'];
}
if ($order['status'] === 'pending') {
$pending_cents += (int) $order['total_cents'];
}
}
return array('paid_cents' => $paid_cents, 'refunded_cents' => $refunded_cents, 'pending_cents' => $pending_cents, 'net_cents' => $paid_cents - $refunded_cents);
}
$lifetime = ogSnippetCustomerLifetimeValueSnapshot(array(array('status' => 'paid', 'total_cents' => 4200), array('status' => 'refunded', 'total_cents' => 700)));
echo $lifetime['net_cents'];