Custom Money Formatting


Setup

You may use your own money formatter. To do so, create a custom formatter-class:

namespace App;

use Happypixels\Shopr\Money\Formatter;

class MyFormatter extends Formatter 
{

}

Make sure your formatter extends the package formatter.

Then, configure your formatter in the money_formatter key of the configuration file:

'money_formatter' => App\MyFormatter::class,

Available options

Now that you have your own formatter set up, you may configure any or all of the following 5 options:

class MyFormatter extends Formatter 
{
    // Which symbol to use when displaying formatted amounts.
    public $symbol = '$';

    // Which side of the amount the symbol should be placed at.
    // Can be either 'before' or 'after'.
    public $symbolPosition = 'before'; 

    // The symbol used as thousand separator.
    public $thousandSeparator = ',';

    // The symbol used for indicating the decimal place.
    public $decimalSeparator = '.';

    // The amount of decimals to enforce. 
    // Note that this will round the amount when defined.
    public $decimalCount = 2;
}

If you leave out any or all of these options, the defaults based on the app locale and configured currency will be used.

Formatting with more freedom

If you don't want to be bound by the options mentioned above, you may instead define a format-method on your formatter:

class MyFormatter extends Formatter 
{
    public function format($amount)
    {
        if ($amount > 1000) {
            return 'Server error, too much money.';
        }

        return '$'.$amount;
    }
}