Ip\Internal\FormatHelper::formatPrice PHP Method

formatPrice() public static method

public static formatPrice ( integer $price, string $currency, string $context, string $languageCode = null ) : string
$price integer Price in cents
$currency string Three letter currency code
$context string
$languageCode string
return string
    public static function formatPrice($price, $currency, $context, $languageCode = null)
    {
        if ($languageCode === null) {
            $languageCode = ipContent()->getCurrentLanguage()->getCode();
        }
        $data = array('price' => $price, 'currency' => $currency, 'context' => $context);
        $formattedPrice = ipJob('ipFormatPrice', $data);
        if ($formattedPrice === null) {
            if (function_exists('numfmt_create') && function_exists('numfmt_format_currency')) {
                $locale = str_replace('-', '_', $languageCode);
                $fmt = numfmt_create($locale, \NumberFormatter::CURRENCY);
                $formattedPrice = numfmt_format_currency($fmt, $price / 100, strtoupper($currency));
                if ($formattedPrice !== false && $formattedPrice != 'NaN') {
                    return $formattedPrice;
                }
            }
            $formattedPrice = round($data['price'] / 100, 2) . ' ' . $data['currency'];
        }
        return $formattedPrice;
    }

Usage Example

Example #1
0
/**
 * Get formatted currency string. If you don't like the way the price is formatted by default, catch ipFormatPrice job and provide your own formatting method.
 *
 * @param int $price Numeric price. Multiplied by 100.
 * @param string $currency Three letter currency code. E.g. "EUR".
 * @param string $context Plugins name that's requesting the operation. This makes it possible to render the price differently for each plugin.
 * @param string $languageCode
 * @return string A currency string in specific country format.
 */
function ipFormatPrice($price, $currency, $context, $languageCode = null)
{
    return \Ip\Internal\FormatHelper::formatPrice($price, $currency, $context, $languageCode);
}