Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Price gets multiplied by 100 when saving product in Spree (RoR)

I installed the online shopping framework Spree on top of Rails 3.1.3 and Ruby 1.9.3. I also use the Spree_i18n gem to localize the shop. Now, whenever I save a product, the price is multiplied by 100.

For example, in the admin area, I type the price 3.20. That results in the value 320. If I save again, it changes to 32000 and so on.

Here is my localized de_numbers.yml for reference:

---
de:
  number:
    currency:
      format:
        format: "%u%n"
        unit: "€"
        precision: 2
        separator: '.'
        delimiter: ','

I can not think of anything unusual in my setup so I wonder why this is not a common problem. Any help will be appreciated.

like image 395
Mark Avatar asked Feb 22 '23 08:02

Mark


2 Answers

EDIT:

spree-core: the Product form is not handling displaying product.price and product.cost_price with regards to I18n / localization. To fix this to work for you, you'll need to modify the core. I'm going to post to the Spree Core team about this, but in the interim, I've tested this fix and it should work.

In /gems/spree_core-1.0.0/app/views/spree/admin/products/_form.html.erb, you will need to modify these lines:

<%= f.text_field :price, :value => number_with_precision(@product.price, :precision => 2) %>

to be this:

<%= f.text_field :price, :value => number_with_precision(@product.price, :precision => I18n.t('number.currency.format.precision'), :separator => I18n.t('number.currency.format.separator'), :delimiter => I18n.t('number.currency.format.delimiter')) %>

and this:

<%= f.text_field :cost_price, :value => number_with_precision(@product.cost_price, :precision => 2) %>

to be this:

<%= f.text_field :cost_price, :value => number_with_precision(@product.cost_price, :precision => I18n.t('number.currency.format.precision'), :separator => I18n.t('number.currency.format.separator'), :delimiter => I18n.t('number.currency.format.delimiter')) %>

Essentially, we're making it handle I18n potential values.

ORIGINAL:

I've duplicated your file exactly, and tried a few tests to recreate this (create a new product, new product variant, change the product price, cost price, etc.). To re-create this, you need to create a de_numbers.yml, and flip your localization to be 'de' in the Spree initializer with "config.default_locale = 'de'"

Here are some suggested fixes:

  1. make sure you run bundle install
  2. in your Gemfile, make sure you're using the latest version of i18n (

gem 'spree_i18n', :git => 'git://github.com/spree/spree_i18n.git')

  1. fix your whitespace to be 2 spaces, instead of tabs (this is potentially a white-space issue where it can't read your i18n values)
  2. Go into the rails console, and log out the values (i.e.

I18n.t('number.currency.format.unit')

  1. Try getting this to work in "en" locale first and then "de".
  2. Put your values into the "de.yml" or "en.yml" first and see if they work before putting into a "de_currency.yml" file.
like image 50
Dominic Tancredi Avatar answered May 05 '23 03:05

Dominic Tancredi


I'm guessing you've transposed the meanings of your separator and delimiter characters. The setup looks correct, so my thinking is that the price needs to be entered as

3,20

Rather than

3.20

This discussion of currency formatting, although not specific to Ruby development, may provide additional insight.

like image 34
Bob Kaufman Avatar answered May 05 '23 02:05

Bob Kaufman