Question

I'm building a site for SpreeCommerce 2.1.3, and I'm stuck trying to figure out how to format the prices correctly.

The currency is DKK (Danish Kroner) and I'm looking for the following:

  • 1000 => kr. 1.000,-
  • 1000.50 => kr. 1.000,50

Spree formats my prices like this:

  • 1000 => kr.1.000,00
  • 1000.50 => kr.1.000,50

So there are two problems:

  1. I need a space between kr. and the price.
  2. When there arent any decimals, I would like the decimals rendered as ,- (example: 1.000,-)

How do I accomplish that?

Here's my configuration from config/initializers/spree.rb:

Spree.config do |config|
  # [...]
  config.currency = "DKK"
  config.currency_symbol_position = "before"
  config.currency_decimal_mark = ","
  config.currency_thousands_separator = "."
end

Solution:

1) I added this decorator to Spree::Money (to replace ,00 with ,-):

Spree::Money.class_eval do
  def to_s
    formatted = @money.format(@options)
    formatted.gsub(/,00$/, ",-")
  end

  def to_html(options = { :html => true })
    to_s
  end
end

2) I configured danish currency in my initializer (to add the space after kr.):

Money::Currency.register({
    :priority        => 1,
    :iso_code        => "DKK",
    :iso_numeric     => "208",
    :name            => "Danish krone",
    :symbol          => "kr. ",
    :subunit         => "Øre",
    :subunit_to_unit => 100,
    :separator       => ".",
    :delimiter       => "," 
})
Was it helpful?

Solution

You can customize https://github.com/spree/spree/blob/master/core/lib/spree/money.rb as you want by overriding it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top