Search by

arraypress / wp-money

arraypress

Integer-minor-unit money formatting and parsing for WordPress. 136 currencies with the correct decimal exponent, so the twenty that are not two decimal places are right.

Package info

github.com/arraypress/wp-money

Homepage

pkg:composer/arraypress/wp-money

Statistics

Installs: 472

Dependents: 4

Suggesters: 3

Stars: 0

Open Issues: 0

v1.0.0 2026-08-25 19:28 UTC

This package is auto-updated.

Last update: 2026-11-18 10:52:39 UTC


README

This library has moved. It is now part of sugarcommerce/money (namespace SugarCommerce\Money, PHP 8.3+). That package's README has a "Moving from arraypress/wp-*" table. This repository is archived and read-only.

WordPress Money

Money as integer minor units, formatted correctly for 136 currencies.

What it does

Storing money as a float is how a total ends up at £9.999999999. Storing it as an integer number of pence is correct, but then every currency needs its own rules: yen has no decimal places, Kuwaiti dinar has three, and Hungarian forint takes a payout only in whole units.

This holds those rules. Amounts go in and out as integers; formatting, parsing and validation know what each currency expects.

Features

  • Format an integer amount for any of 136 currencies, with the right decimals
  • Parse a typed amount back to minor units, whatever separators were used
  • Check an amount is chargeable, and say why it is not
  • Render a sale price with the old price struck through and a screen-reader label
  • Work out a saving percentage, or apply a flat or percentage discount
  • Split an amount between parts without losing a penny to rounding
  • Describe a subscription interval — "/month", "every 3 weeks"
  • Global helpers for the common cases: format_money(), sanitize_money()
  • Clean an amount or an exchange rate on its way into storage (Money::sanitize_amount(), Money::sanitize_exchange_rate()): never negative, never an exception

Installation

composer require arraypress/wp-money

Quick start

use ArrayPress\Money\Currencies;
use ArrayPress\Money\Money;
use ArrayPress\Money\Recurring;

// 4999 pence, in the store's currency.
echo Money::format( 4999 );                      // £49.99
echo Money::format( 4999, [ 'currency' => 'JPY' ] );  // ¥4,999  (no decimals)

// What the customer typed, back to minor units.
$amount = Money::parse( '1,299.50', 'GBP' );     // 129950

// Would the gateway take this?
if ( ! Money::is_chargeable( $amount, 'GBP' ) ) {
    $why = Money::why_not_chargeable( $amount, 'GBP' );
}

// A sale price, marked up for a listing.
echo render_price( 3999, [ 'compare_at' => 4999 ] );

// For a spreadsheet or an input: every decimal place the currency has, no commas.
echo Money::decimal( 1234567, 'KWD', false );    // 1234.567
echo Money::input_value( 123450, 'USD' );        // 1234.50

// A currency select, labelled your way.
$options = Currencies::options( '{name} ({code})' );  // 'USD' => 'US Dollar (USD)'

// How a price bills.
echo Recurring::label( 'month', 3 );                                   // Every 3 months
echo Recurring::with_amount( '£35.99', 'month', 1, Recurring::LONG );  // £35.99 / month
echo Recurring::with_amount( '$9.99', 'month' );                       // $9.99/mo
[ $interval, $count ] = Recurring::parse_period( '3 months' );         // 'month', 3

Requirements

  • PHP 8.3 or later
  • WordPress 7.1 or later

License

GPL-2.0-or-later