forked from OFFLINE-GmbH/oc-mall-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
44 lines (39 loc) · 1.2 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
use OFFLINE\Mall\Models\CurrencySettings;
use OFFLINE\Mall\Models\Product;
if ( ! function_exists('round_money')) {
/**
* Rounds an amount to the specified decimals.
*/
function round_money($value, $decimals = 2)
{
return round($value / 100, $decimals ?? 2);
}
}
if ( ! function_exists('format_money')) {
/**
* Formats a price. Adds the currency if provided.
*
* @param int $value
* @param Product|null $product
* @param null $currencyCode
*
* @return string
*/
function format_money(?int $value, $product = null, $currencyCode = null)
{
$currency = $currencyCode
? CurrencySettings::currencyByCode($currencyCode)
: CurrencySettings::activeCurrency();
$value = round_money($value, $currency['decimals']);
$integers = floor($value);
$decimals = ($value - $integers) * 100;
return Twig::parse($currency['format'], [
'price' => $value,
'integers' => $integers,
'decimals' => str_pad($decimals, 2, '0', STR_PAD_LEFT),
'product' => $product,
'currency' => $currency,
]);
}
}