wagnerwagner / merx

Merx is a plugin to create online shops with Kirby.
https://merx.wagnerwagner.de
104 stars 10 forks source link

Currency symbol before number #2

Closed guidoferreyra closed 4 years ago

guidoferreyra commented 4 years ago

Hi! I was looking the code of Merx to set the symbol before the number, and I found that is related with localeconv(), but sometimes this is not desired. Maybe it will be better to configure this via config options.

Here a demo code, maybe explain better what I’m thinking.

   public static function formatPrice(float $price): string
    {
        $localeFormatting = localeconv();
        $precedes = option('ww.merx.currencySymbolBefore', true);
        $string = '';
        if ($precedes) {
            $string .= option('ww.merx.currencySymbol', '€');
            if (option('ww.merx.currencySpace')) {
                $string .= ' '; // non breaking space
            }
        }
        $string .= number_format($price, 2, $localeFormatting['decimal_point'] ?? '.', $localeFormatting['thousands_sep'] ?? ',');
        if (!$precedes) {
            if (option('ww.merx.currencySpace')) {
                $string .= ' '; // non breaking space
            }
            $string .= option('ww.merx.currencySymbol', '€');
        }
        return $string;
    }
tobiasfabian commented 4 years ago

I’ve updated the formatPrice function with the latest release. What do you think. Is this everything you need?

https://merx.wagnerwagner.de/docs/classes/merx#formatprice

guidoferreyra commented 4 years ago

Looks great, I will try it. Thanks a lot!

guidoferreyra commented 3 years ago

Hi @tobiasfabian, what do you think on adding an argument to control the number of decimals?