The SDK includes standalone utility classes for currency formatting and timezone conversion. These work with data already returned by the API — no extra HTTP requests needed.
CoCart's API returns prices as smallest-unit integers (e.g., 4599 for $45.99) along with currency metadata. CurrencyFormatter converts these into human-readable strings.
use CoCart\CurrencyFormatter;
$formatter = new CurrencyFormatter();
// With a specific locale (requires ext-intl)
$formatter = new CurrencyFormatter('de_DE');$response = $client->cart()->get();
$currency = $response->getCurrency();
// Full formatted price with currency symbol
echo $formatter->format(4599, $currency); // "$45.99"
// Plain decimal (no symbol)
echo $formatter->formatDecimal(4599, $currency); // "45.99"Currencies like JPY have no minor units. The formatter handles this automatically:
// JPY currency info from the API
$currency = $response->getCurrency();
// currency_minor_unit = 0
echo $formatter->format(1500, $currency); // "¥1,500"
echo $formatter->formatDecimal(1500, $currency); // "1500"- If the
intlPHP extension is installed, the formatter usesNumberFormatter::formatCurrency()for proper locale-aware output (e.g.,1.234,56 €in German locale). - If
intlis not available, it falls back to using thecurrency_prefix,currency_suffix,currency_decimal_separator, andcurrency_thousand_separatorvalues from the API response.
The API response includes this currency structure:
$currency = $response->getCurrency();
// [
// 'currency_code' => 'USD',
// 'currency_symbol' => '$',
// 'currency_minor_unit' => 2,
// 'currency_decimal_separator' => '.',
// 'currency_thousand_separator' => ',',
// 'currency_prefix' => '$',
// 'currency_suffix' => '',
// ]$response = $client->cart()->get();
$currency = $response->getCurrency();
$formatter = new CurrencyFormatter();
foreach ($response->getItems() as $item) {
$name = $item['name'];
$price = $formatter->format($item['totals']['total'], $currency);
echo "{$name}: {$price}\n";
}
$totals = $response->getTotals();
echo "Subtotal: " . $formatter->format($totals['subtotal'], $currency) . "\n";
echo "Total: " . $formatter->format($totals['total'], $currency) . "\n";WooCommerce stores dates in the store's configured timezone (often UTC). TimezoneHelper converts these to any other timezone.
use CoCart\TimezoneHelper;
$tz = new TimezoneHelper();echo $tz->detectTimezone(); // "America/New_York"// Convert a UTC date to New York time
$local = $tz->convert('2025-01-15T15:00:00', 'UTC', 'America/New_York');
echo $local; // "2025-01-15T10:00:00"
// Convert between any two timezones
$tokyo = $tz->convert('2025-07-15T12:00:00', 'Europe/London', 'Asia/Tokyo');
echo $tokyo; // "2025-07-15T20:00:00"Shorthand to convert from the store's timezone to the system's timezone:
// Store is in UTC (default)
$local = $tz->toLocal('2025-01-15T15:00:00');
// Store is in a specific timezone
$local = $tz->toLocal('2025-01-15T10:00:00', 'America/Chicago');$tz = new TimezoneHelper();
$response = $client->cart()->get();
$expiryDate = $response->get('expiry.date');
if ($expiryDate) {
$localExpiry = $tz->toLocal($expiryDate, 'UTC');
echo "Cart expires: {$localExpiry}";
}