Shopify Liquid - Convert number variable to string

Solved

Shopify Liquid - Convert number variable to string

PKontopoulos
Shopify Partner
5 0 1

Hello all,

 

We want to add a tracking script for a platform we work with (analytics purposes, order logging). Their script requires some fields, like order id, total order cost, etc, but all fields should be in string format.

 

What's the proper way to convert, for example, number values to string?

 

Posting a complete example, in order to give you all a better idea of what we're dealing with:

bp('addOrder', {
  orderId:  '123456',                        // Order ID (alias: order_id)
  revenue:  '1315.25',                       // Grand Total (Cost + Tax + Shipping)
  shipping: '5.45',                          // Shipping Cost
  tax:      '301.25',                        // Tax
  currency: 'euro',
});

 

For example, 'shipping' would be 'shipping_price | money_without_currency', but how can we convert this to string format?

 

Any help would be greatly appreciated, Shopify Support was unable to answer this seemingly simple question.

Accepted Solution (1)
Havik
Excursionist
14 3 7

This is an accepted solution.

Owh, you can actually just pass it directly, as such,

 

bp('addOrder', {
  orderId: "{{ checkout.orderId }}",                        
  revenue:  "{{ checkout.revenue | money_without_currency  }}",                       
  shipping: "{{ checkout.shipping | money_without_currency  }}",                          
  tax:      "{{ checkout.tax | money_without_currency  }}",                        
  currency: 'euro',
});

is this what you wanted? 

View solution in original post

Replies 6 (6)

zaczee
Globetrotter
855 46 44

Hi,

 

Can you share your store url

 

PKontopoulos
Shopify Partner
5 0 1

Havik
Excursionist
14 3 7

Hai, the short answer for your question is no function to convert, string to numbers or vice versa in liquid. You can refer it here, https://www.shopify.com/ca/partners/shopify-cheat-sheet 

 

I assume the analytics code is in a script tag. So, like what we did on ours is to parseInt the string from liquid object and pass that to the analytics code. For example,

 

 

var orderId = parseInt("{{checkout.orderId}}");
var revenue = parseInt("{{checkout.revenue | money_without_currency }}");
var shipping = parseInt("{{checkout.shipping | money_without_currency}}");
var tax = parseInt("{{checkout.tax | money_without_currency }}");

bp('addOrder', {
  orderId:  orderId,                        
  revenue:  revenue,                       
  shipping: shipping,                          
  tax:      tax,                        
  currency: 'euro',
});

 

 

Hope this helps.

PKontopoulos
Shopify Partner
5 0 1

First of all thank you for taking the time to answer.

 

If I understand correctly, parseInt is used to convert string to int, what if we need to do the exact opposite (number to string), as described in the question?

Havik
Excursionist
14 3 7

This is an accepted solution.

Owh, you can actually just pass it directly, as such,

 

bp('addOrder', {
  orderId: "{{ checkout.orderId }}",                        
  revenue:  "{{ checkout.revenue | money_without_currency  }}",                       
  shipping: "{{ checkout.shipping | money_without_currency  }}",                          
  tax:      "{{ checkout.tax | money_without_currency  }}",                        
  currency: 'euro',
});

is this what you wanted? 

PKontopoulos
Shopify Partner
5 0 1

Yes, quotation marks seem to have done the trick. Thanks a lot!