0

I'm using Middleman and Snipcart to develop an e-commerce project. I need to use multi-currency, the Snipcart documentation says I just have to set in my buy-button a data-item-price with a JSON array like {"usd": 20, "eur": 25}. There's others dataset to put on the buy-button, so I decided to make a helper.

This helper return exactly what I want, but the data-item-price is between double quotes and not between single quotes. For the moment, the code is understand as data-item-price="{"usd":20,"eur":22}" and not as data-item-price='{"usd": 20, "eur": 22}'

I need to transform <button data-item-price="{"usd":20,"eur":22}">Buy</button> by this <button data-item-price='{"usd":20,"eur":22}'>Buy</button>.

Here's the snipcart (buy) button helper:

def snipcart_button (p, text)
  args = {
    "class" => "snipcart-add-item",
    "data-item-id" => p.id,
    "data-item-price" => p.price.to_json,
    "data-item-name" => p[locale].name,
    "data-item-max-quantity" => p.max_quantity,
    "data-item-url" => snipcart_product_url(p),
    "data-item-image" => p.image
  }

  content_tag :button, args do
    text
  end
end
Bastien Robert
  • 809
  • 1
  • 10
  • 31

2 Answers2

2

What you are looking for is probably not double quotes to single quotes, but this helper: https://apidock.com/rails/ActionView/Helpers/JavaScriptHelper/escape_javascript

This will escape double and single quotes.

"data-item-price" => j(p.price.to_json),
smallbutton
  • 3,377
  • 15
  • 27
  • Using `j` helper (or `escape_javascript`) returns `data-item-price="{\"usd\":20,\"eur\":22}"`, not understood anymore by Snipcart. The error `Uncaught SyntaxError: Unexpected token N in JSON at position 0` is displayed on the console. – Bastien Robert Dec 30 '17 at 20:51
  • `data-item-price="{\"usd\":20,\"eur\":22}"` looks to me like it should now be a valid json attribute. Maybe the error is on the receiving end? – smallbutton Jan 03 '18 at 17:00
  • Also there is no `N` in the output string, which the error message suggests. So maybe the error comes from something else now? – smallbutton Jan 03 '18 at 17:01
  • I'm currently looking with the Snipcart support, try everything to fix the problem. The JSON is understand by the cart using `p.price.to_json.to_s` or `p.price.to_json.safe_html` but not by the Snipcart callback on order validation. – Bastien Robert Jan 04 '18 at 01:54
  • Okay, the problem was from Snipcart, so both solutions (your and krawitz) worked after they fix the parsing bug. Thanks ! – Bastien Robert Jan 06 '18 at 14:27
1

Does this work ?

def snipcart_button(p, text)
  content_tag(:button, text, {
    class: 'snipcart-add-item',
    data: {
      'item-id': p.id,
      'item-price': p.price.to_json.html_safe,
      'item-name': p[locale].name,
      'item-max-quantity': p.max_quantity,
      'item-url': snipcart_product_url(p),
      'item-image': p.image
    }
  })
end

The (very) ugly way would be:

def snipcart_button(p, text)
  content_tag(:button, text, {
    class: 'snipcart-add-item',
    data: {
      'item-id': p.id,
      'item-price': '',
      'item-name': p[locale].name,
      'item-max-quantity': p.max_quantity,
      'item-url': snipcart_product_url(p),
      'item-image': p.image
    }
  }).sub(/(data-item-price=)""/, "\\1'#{p.price.to_json.html_safe}'").html_safe
end
Julien Dargelos
  • 356
  • 1
  • 12