[英]Can't change currency symbol on spree 3.0
I'm currently using Spree 3.0 and changed my currency to Colombian Peso (COP). 我目前正在使用Spree 3.0,并将我的货币更改为哥伦比亚比索(COP)。 Right now prices are shown like this: ₱80.000,00.
现在的价格如下所示:₱80.000,00。 I want them to look like this: $ 80.000.
我希望他们看起来像这样: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 $.
我对如何删除逗号后的两个0有一个想法,但是还不能更改$的货币符号。 This is what I have so far:
这是我到目前为止的内容:
in /config/initializers/spree.rb 在/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 $. 我添加了以下内容,现在我在逗号后删除了两个0,但在这样的价格后也得到了$: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
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. 使用@luisjar答案,您实际上并不使用COP货币的子单位,但某些付款网关(如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.
为了将COP货币(或任何其他货币)与一个子单元一起使用而不显示它,您需要将格式属性no_cents设置为true。 This is the way I show the amount in Colombian Peso currency like $ 10,000 COP.
这是我以哥伦比亚比索显示的金额(如$ 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. 我希望这能对其他与Spree中的货币混淆的人(如我)有所帮助。
You can read more about currency format in: https://github.com/RubyMoney/money/blob/master/lib/money/money/formatting.rb 您可以在以下网址中了解有关货币格式的更多信息: https : //github.com/RubyMoney/money/blob/master/lib/money/money/formatting.rb
With the code, you merely register another currency. 使用该代码,您只需注册另一种货币。 Each product(-variant) has many prices, one per currency.
每个产品(变体)都有许多价格,每种货币一种。 You should make sure, probably in a migration, that all your prices on all your variants are updated to use the correct currency.
您应该确保(可能是在迁移中)所有变体的所有价格都已更新为使用正确的货币。
Spree::Price.update_all(currency: 'COP')
If you don't want to loose the Dollar prices, but instead want to add an additional price, you need to edit each one manually, or on a migration. 如果您不想放宽美元价格,而是想增加一个额外的价格,则需要手动或在迁移时对每个价格进行编辑。
Spree::Price.find_each do |price|
Spree::Price.create(variant: price.variant, amount: price.amount * 1.337, currency: 'COP')
end
Where 1.337
is a conversion factor. 其中
1.337
是转换因子。
I finally found how to remove the two ceros after the comma, use the $ symbol but still use the Colombian peso Currency. 我终于找到了如何在逗号后删除两个ceros,使用$符号,但仍然使用哥伦比亚比索货币。 This is the way I did it.
这就是我做到的方式。
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
Spree::Money.class_eval do
def to_s
@money.format.gsub(/,00/, "")
@money.format(:symbol_position => :before)
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 => "Peso",
:subunit_to_unit => 1,
:separator => ".",
:delimiter => ","
})
end
Looking at documentation of class Currency (in gems/money_6.9.0/lib/money/currency.rb) I found better the following settings: 查看货币类的文档(位于gems / money_6.9.0 / lib / money / currency.rb中),我发现更好的以下设置:
Money::Currency.register({
:priority => 1,
:iso_code => "COP",
:iso_numeric => country.id,
:name => "Peso",
:symbol => "$",
:html_entity => "$ ",
:symbol_first => true,
:subunit => "Cent",
:subunit_to_unit => 100,
:separator => ".",
:delimiter => ","
})
The "name" attribute is the name of the currency, not the name of the country. “名称”属性是货币名称,而不是国家名称。 The space between the $ and the number goes inside the "html_entity" and not in the symbol.
$和数字之间的空格位于“ html_entity”内部,而不位于符号中。 Finally, the "symbol_first" attribute controls where to place the symbol.
最后,“ symbol_first”属性控制放置符号的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.