1. Go to this page and download the library: Download whitecube/php-prices library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
whitecube / php-prices example snippets
use Whitecube\Price\Price;
$steak = Price::EUR(1850) // Steak costs €18.50/kg
->setUnits(1.476) // Customer had 1.476kg, excl. total is €27.31
->setVat(6) // There is 6% VAT, incl. total is €28.95
->addTax(50) // There also is a €0.50/kg tax (before VAT), incl. total is €29.73
->addDiscount(-100); // We granted a €1.00/kg discount (before VAT), incl. total is €28.16
use Brick\Money\Money;
use Whitecube\Price\Price;
$base = new Money::ofMinor(500, 'USD'); // $5.00
$single = new Price($base); // 1 x $5.00
$multiple = new Price($base, 4); // 4 x $5.00
use Whitecube\Price\Price;
$major = Price::of(5, 'EUR'); // 1 x €5.00
$minor = Price::ofMinor(500, 'USD'); // 1 x $5.00
use Whitecube\Price\Price;
$single = Price::EUR(500); // 1 x €5.00
$multiple = Price::USD(500, 4); // 4 x $5.00
use Whitecube\Price\Price;
$guessCurrency = Price::parse('5,5$'); // 1 x $5.50
$betterGuess = Price::parse('JMD 5.50'); // 1 x $5.50
$forceCurrency = Price::parse('10', 'EUR'); // 1 x €10.00
$multiple = Price::parse('6.008 EUR', null, 4); // 4 x €6.01
$force = Price::parse('-5 EUR', 'USD', 4); // 4 x $-5.00
use Whitecube\Price\Price;
$price = Price::ofMinor(500, 'USD')->setUnits(2); // 2 x $5.00
$price->minus('2.00') // 2 x $3.00
->plus('1.50') // 2 x $4.50
->dividedBy(2) // 2 x $2.25
->multipliedBy(-3) // 2 x $-6.75
->abs(); // 2 x $6.75
use \Brick\Money\Money;
use \Whitecube\Price\Price;
use \Brick\Math\RoundingMode;
$base = Money::ofMinor(1000, 'EUR');
$price = new Price($base);
// A rounding mode is mandatory in order to do the division,
// which causes rounding errors down the line
$price->dividedBy(12, RoundingMode::HALF_UP)->multipliedBy(11);
$price->getMinorAmount(); // 913 minor units ❌
use \Brick\Money\Money;
use \Whitecube\Price\Price;
use \Brick\Math\RoundingMode;
use \Brick\Money\Context\CustomContext;
$base = Money::ofMinor(1000, 'EUR')->toRational();
$price = new Price($base);
// With RationalMoney, rounding is not necessary at this stage
$price->dividedBy(12)->multipliedBy(11);
// But rounding can occur at the very end
$price->to(new CustomContext(2), RoundingMode::HALF_UP)->getMinorAmount(); // 917 minor units ✅
use Whitecube\Price\Price;
use Brick\Money\Money;
$price = new Price(Money::ofMinor(500, 'EUR'), 2); // 2 units of €5.00 each
$same = Price::EUR(500, 2); // same result
$again = Price::parse('5.00', 'EUR', 2); // same result
$price->setUnits(1.75); // 1.75 x €5.00
$quantity = $price->units(); // 1.75
use Whitecube\Price\Price;
$price = Price::USD(200); // 1 x $2.00
$price->setVat(21); // VAT is now 21.0%, or $0.42 per unit
$price->setVat(null); // VAT is unset
use Whitecube\Price\Price;
use Brick\Money\Money;
$price = Price::USD(800, 5) // 5 x $8.00
->addDiscount(-100) // 5 x $7.00
->addDiscount(Money::of(-5, 'USD')); // 5 x $6.50
use Whitecube\Price\Price;
use Brick\Money\Money;
$price = Price::EUR(125, 10) // 10 x €1.25
->addTax(100) // 10 x €2.25
->addTax(Money::of(0.5, 'EUR')); // 10 x €2.75
use Whitecube\Price\Price;
use Brick\Money\Money;
$price = Price::USD(2000) // 1 x $20.00
->addModifier('coupon', -500) // 1 x $15.00
->addModifier('extra', Money::of(2, 'USD')); // 1 x $17.00
use Whitecube\Price\Price;
$price = Price::EUR(600, 5)
->addDiscount(Discounts\FirstOrder::class)
->addTax(Taxes\Gambling::class)
->addModifier('custom', SomeCustomModifier::class);
use Brick\Money\AbstractMoney;
use Brick\Math\RoundingMode;
use Whitecube\Price\Modifier;
use Whitecube\Price\PriceAmendable;
class SomeRandomModifier implements PriceAmendable
{
/**
* The current modifier "type"
*
* @return string
*/
protected string $type;
/**
* Return the modifier type (tax, discount, other, ...)
*
* @return string
*/
public function type(): string
{
return $this->type;
}
/**
* Define the modifier type (tax, discount, other, ...)
*
* @param null|string $type
* @return $this
*/
public function setType(?string $type = null): static
{
$this->type = $type;
return $this;
}
/**
* Return the modifier's identification key
*
* @return null|string
*/
public function key(): ?string
{
return 'very-random-tax';
}
/**
* Get the modifier attributes that should be saved in the
* price modification history.
*
* @return null|array
*/
public function attributes(): ?array
{
return [
'subtitle' => 'Just because we don\'t like you today',
'color' => 'red',
];
}
/**
* Whether the modifier should be applied before the
* VAT value has been computed.
*
* @return bool
*/
public function appliesAfterVat(): bool
{
return false;
}
/**
* Apply the modifier on the given Money instance
*
* @param \Brick\Money\AbstractMoney $build
* @param float $units
* @param bool $perUnit
* @param null|\Brick\Money\AbstractMoney $exclusive
* @param null|\Whitecube\Price\Vat $vat
* @return null|\Brick\Money\AbstractMoney
*/
public function apply(AbstractMoney $build, float $units, bool $perUnit, AbstractMoney $exclusive = null, Vat $vat = null): ?AbstractMoney
{
if(date('j') > 1) {
// Do not apply if it's not the first day of the month
return null;
}
// Otherwise add $2.00 per unit
$supplement = Money::of(2, 'EUR');
return $build->plus($perUnit ? $supplement : $supplement->multipliedBy($units, RoundingMode::HALF_UP));
}
}
use Brick\Money\Money;
use Whitecube\Price\Price;
$price = Price::EUR(600, 5)
->addModifier('lucky-or-not', BetweenModifier::class, Money::ofMinor(-100, 'EUR'), Money::ofMinor(100, 'EUR'));
use Brick\Money\Money;
use Whitecube\Price\PriceAmendable;
class BetweenModifier implements PriceAmendable
{
protected $minimum;
protected $maximum;
public function __construct(Money $minimum, Money $maximum)
{
$this->minimum = $minimum;
$this->maximum = $maximum;
}
// ...
}
use Whitecube\Price\Price;
$price = Price::USD(800, 5)->addTax(function($tax) {
$tax->add(200)->setPostVat();
});
use Whitecube\Price\Price;
Price::formatUsing(fn($price, $locale = null) => /* Convert $price to a string for $locale */);
// or
Price::formatUsing(\App\Formatters\MyPriceFormatter::class);
// or
Price::formatUsing(new \App\Formatters\MyPriceFormatter($some, $dependencies));
use Whitecube\Price\Price;
setlocale(LC_ALL, 'en_US');
Price::formatUsing(fn($price, $locale = null) => $price->exclusive()->getMinorAmount()->toInt())
->name('rawExclusiveCents');
Price::formatUsing(\App\Formatters\MyInvertedPriceFormatter::class)
->name('inverted');
$price = Price::EUR(600, 8)->setVat(21);
echo Price::formatRawExclusiveCents($price); // 4800
echo Price::formatInverted($price); // -€58.08
// When using named formatters the default formatter stays untouched
echo Price::format($price); // €58.08