Search code examples
ruby-on-railsspreecurrency

Can't change currency symbol on spree 3.0


I'm currently using Spree 3.0 and changed my currency to Colombian Peso (COP). Right now prices are shown like this: ₱80.000,00. I want them to look like this: $ 80.000. I have an idea on how to remove the two 0 after the comma but haven't been able to change the currency symbol for $. This is what I have so far:

in /config/initializers/spree.rb

Spree.config do |config|

  config.logo = 'templo samadhi logo.png'
  config.admin_interface_logo = 'templo samadhi logo.png'
  country = Spree::Country.find_by_name('Colombia')
  config.default_country_id = country.id if country.present?
  config.checkout_zone = country.id

Money::Currency.register({
   :priority        => 1,
   :iso_code        => "COP",
   :iso_numeric     => country.id,
   :name            => "Colombia",
   :symbol          => "$ ",
   :subunit         => "Cent",
   :subunit_to_unit => 100,
   :separator       => ".",
   :delimiter       => ","
})
end

This is somehow working because the ₱ symbol is getting removed but the $ is not showing up.

I appreciate if someone can help me with this.

UPDATE

I added the following and now I'm removing the two 0 after the comma but I'm also getting a $ after the price like this: 80.000 $.

So right now I don't know how to move the $ symbol before the price.

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

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

Solution

  • I was using this solution but found an issue when I try to process the payment through the stripe gateway, it actually needs the amount subunit to be "Cents" in order to make the currency conversion.

    With the @luisjar answer you actually don't use subunits in the COP currency but they may be needed for some payment gateways like stripe. In order to use the COP currency (or any other currency) with a subunit but without showing it, you need to set the format property no_cents true. This is the way I show the amount in Colombian Peso currency like $ 10,000 COP.

    Spree.config do |config|
    
      #Change currency symbol for Colombia
      country = Spree::Country.find_by_name('Colombia')
      config.default_country_id = country.id if country.present?
      config.checkout_zone = country.id
    
    
      Spree::Money.class_eval do
        def to_s
          @money.format.gsub(/,00/, "")
          @money.format(:symbol_position => :before, :with_currency => true, :no_cents => true)
        end
    
        def to_html(options = { :html => true })
         to_s
        end
      end
    
      Money::Currency.register({
        :priority        => 1,
        :iso_code        => "COP",
        :iso_numeric     => country.id,
        :name            => "Colombia",
        :symbol          => "$ ",
        :subunit         => "Cent",
        :subunit_to_unit => 100,
        :separator       => ".",
        :delimiter       => ","
      })
    
      Spree::Price.update_all(currency: 'COP')
    end
    

    I hope this help to any other confused (like me) with the currencies in Spree.

    You can read more about currency format in: https://github.com/RubyMoney/money/blob/master/lib/money/money/formatting.rb