Changing the currency format for product prices within Spree
Asked Answered
D

5

5

I'm upgrading spree to from spree 1.0 to 1.3 and get stuck with the new currency options.

I want to render prices as: '€ 100' but instead get '€100'. How do I get a space between the unit and the value?

Note: Changing the locale file doesn't work, since it uses the money gem.

Doff answered 17/10, 2013 at 14:21 Comment(0)
S
6

There are a bunch of ways to do this. The easiest would probably be to re-register the Euro currency with a different symbol.

Put the following in an initializer:

# encoding: utf-8
Money::Currency.register({
    :priority        => 1,
    :iso_code        => "EUR",
    :iso_numeric     => "978",
    :name            => "Euro",
    :symbol          => "€ ",
    :subunit         => "Cent",
    :subunit_to_unit => 100,
    :separator       => ".",
    :delimiter       => "," 
})

A rails console now reports:

> Spree::Money.new(100, currency: 'EUR')
=> € 100.00 
Steep answered 17/10, 2013 at 15:47 Comment(0)
G
1

I did the following in my config/initializers/spree.rb to inject a different symbol:

Money::Currency.table[:chf].merge!(symbol: 'CHF ')

This way the currencies aren't going to mix up.

Giess answered 28/8, 2014 at 9:18 Comment(0)
D
0

Thanks a lot. In my case, used the following to change the symbol generated by the to_html method, in case anyone has the same problem.

# encoding: utf-8
Money::Currency.register({
    :priority        => 1,
    :iso_code        => "CLP",
    :iso_numeric     => "152",
    :name            => "Chilean Peso",
    :symbol          => "$",
    :subunit         => "Peso",
    :subunit_to_unit => 1,
    :separator       => ",",
    :delimiter       => ".",
    html_entity: "$"
})
Delorsedelos answered 29/11, 2013 at 15:18 Comment(0)
C
0

I solved the problem with the following in an initializer, e.g. config/initializers/currency_formatting.rb:

# Display prices with a space between symbol and number:
Spree::Money.default_formatting_rules[:symbol_before_without_space] = false

This hooks into the formatting rules found in Spree::Money, which can control all the formatting Options of the Money Gem, including the one placing a space between the symbol and the number. This has the advantage over the other solutions presented here that it works with all currencies at once.

Confusion answered 17/8, 2016 at 14:11 Comment(0)
S
0

Okay, this was pretty easy. As of gem version money(6.16.0). In Spree intializer spree.rb Recommended way:

Spree.config do |config|
  ...  
  ...

  # Below is deprecated 
  Spree::Money.default_formatting_rules[:symbol_before_without_space] = false

  # Instead try this 
  Spree::Money.default_formatting_rules[:format] = '%u %n'

end
Subtorrid answered 5/4, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.