Solved

Is there a filter available to Format an item price to include a comma in Klaviyo?

  • 26 January 2022
  • 11 replies
  • 1296 views

Badge +2

Hi,

 

We struggle to find a way to format item price values with comma every three digits (before the float point).

 

For example:

  • 10283 → should become 10,283.00
  • 2938449 → should become 2,938,449.00

Is there a way to do such format, or is it something can’t be achieved with current django string and float format functions in Klaviyo? knowing that django’s intcomma allows such formatting but apparently the function is not supported in Klaviyo.

 

Thanks,

Loai

icon

Best answer by Taylor Tarpley 28 January 2022, 21:11

View original

11 replies

Userlevel 7
Badge +60

Hi @loai.taha

 

Welcome to the Community! Thanks for sharing your question with us! 

 

Yes, Klaviyo does support Django filters! While there are too many to list in our help center documentation,, we often direct users to this resources as well as it is a more extensive list of available Django filters available. While I suggest you peruse the document for yourself, I do believe there are available filters that deal with decimal and comma placements. 

 

However, if you do not find the Django filter you’re looking for presently, or in the future, there is also the option of changing how your item is priced on your website. When your website is integrated with Klaviyo, we receive whatever information is formatted on your site and reflect it in our application. I would suggest double checking whether or not it’s  possible to send the data in the format you want with your specific integration, however, if you were to change the backend of your website to include commas in your items price, you would not need to use a filter or for anything to be changed or added to the price.  This could be another alternative workaround to achieve what you’re looking for! 

 

Thank you for participating in the Community!

-Taylor 

Badge +2

Greetings @Taylor Tarpley ,

 

Thanks for your reply. Much appreciated.

 

We can’t do the change in the database, it will cause a series of data integrity issues, especially when you run a site for different countries and locales. The solution is to format product price on the front-end (which we do that already on our website).

 

The source you have shared doesn’t touch on numbers formatting, or at least, doesn’t cover my case. 

 

As I mentioned before, django’s intcomma function is what can be used in django to format monetary values into comma separated (1234.00 → 1,234.00). However, when I tried using it in the email template, it didn’t work.

 

Here is the code I used according to django’s docs: {{item.Price|intcomma}}

 

When testing the template, I get the following error:

 

Nothing is clear about the error. And apparently intcomma is not part of the django library that klaviyo use, or could be something else.

 

Now are you able to spot the problem, or perhaps, provide an alternative solution? Or should I consider it a limitation in Klaviyo?

 

Thanks,

Userlevel 7
Badge +60

Hi @loai.taha

 

Thank you for brining this to my attention and attaching a screenshot! It was very helpful to bring to my colleagues and discuss further. After more investigation, it does seem like this is a limitation of our editor. You could use this filter to accomplish what you’re looking for, however this filter only works as long as the numbers are six digits and below:

{% with val=event|lookup:'$value' %} {% if val|where:'val > 999' %} ${{ val|stringformat:"d"|slice:":-3" }},{{ val|stringformat:"d"|slice:"-3:" }} {% else %}{{ val|stringformat:"d" }}{% endif %} {% endwith %}

 

However, I am looping in more specialist and will try to discover if there is another workaround for you! Very sorry for the inconvineince, but thank you for bringing this to the Community’s attention! 

-Taylor 

Userlevel 1
Badge +2

Hi,

I’m using the code that Taylor posted — with some minor tweaks. I use “item.LineSubTotal” rather than “event|lookup:'$value'”. And I want two decimal places so I use “floatformat:2” instead of ‘stringformat:"d"’. See below:

{% with val=item.LineSubTotal %} {% if val|where:'val > 999' %} ${{ val|floatformat:2|slice:":-6" }},{{ val|floatformat:2|slice:"-6:" }} {% else %}{{ val|floatformat:2 }}{% endif %} {% endwith %}
 

The if statement doesn’t appear to be functioning correctly, since I get $,300.00 rather than $300.00!

Amounts larger than a thousand look correct.

Any insights?

Thanks!

Wolfgang

Badge

Hi,

I’m using the code that Taylor posted — with some minor tweaks. I use “item.LineSubTotal” rather than “event|lookup:'$value'”. And I want two decimal places so I use “floatformat:2” instead of ‘stringformat:"d"’. See below:

{% with val=item.LineSubTotal %} {% if val|where:'val > 999' %} ${{ val|floatformat:2|slice:":-6" }},{{ val|floatformat:2|slice:"-6:" }} {% else %}{{ val|floatformat:2 }}{% endif %} {% endwith %}
 

The if statement doesn’t appear to be functioning correctly, since I get $,300.00 rather than $300.00!

Amounts larger than a thousand look correct.

Any insights?

Thanks!

Wolfgang

Having the same problem

Badge
{% with val=feeds.feedname.0.price %}${% if val > 999 %}{{ val|stringformat:"d"|slice:":-3" }},{{ val|stringformat:"d"|slice:"-3:" }} {% else %}{{ val|stringformat:"d" }}{% endif %} {% endwith %}

Here’s a solve for anyone using a non-event lookup value @Wolfgang 

Userlevel 1
Badge +2

Hi Amoncrief,

Thanks a million. This does the trick!

Badge

Hi,

I am having the same issue, the price shows up as $7000 instead of $70.00

I have tried all of the above solutions, but I am not able to get $70.00, all I am getting is $7,000.00

Here are the two different version of the code I tried.

Version 1:

{% with val=item.line_price %} {% if val|where:'val > 999' %} ${{ val|stringformat:"d"|slice:":-3" }},{{ val|stringformat:"d"|slice:"-3:" }} {% else %}{{ val|stringformat:"d" }}{% endif %} {% endwith %}

Version 2:

{% with val=item.line_price %}${% if val > 999 %}{{ val|floatformat:2|slice:":-6" }},{{ val|floatformat:2|slice:"-6:" }} {% else %}{{ val|floatformat:2 }}{% endif %} {% endwith %}

I am obviously doing something wrong. Can someone please provide a solution?

Thank you 

 

Badge

Hello again, 

Right after asking the above question I was able to find a solution for the problem I was having.

{{ item.line_price|divide:100|floatformat:2 }}

Please let me know if there might be any issues with this solution.

Thank you

Userlevel 1
Badge +2

Hi,

In the meantime I have found the filter that accomplishes the desired effect. As stated in the Klaviyo docs most Django filters are supported and as of Django 3.2:

The g suffix to force grouping by thousand separators was added to floatformat.

https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#floatformat

So, all that’s needed is to run your value through the following filter:

{{ val|floatformat:”2g” }}

This simplifies things considerably.

Badge

Hi,

In the meantime I have found the filter that accomplishes the desired effect. As stated in the Klaviyo docs most Django filters are supported and as of Django 3.2:

The g suffix to force grouping by thousand separators was added to floatformat.

https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#floatformat

So, all that’s needed is to run your value through the following filter:

{{ val|floatformat:”2g” }}

This simplifies things considerably.

You are a life saver!!! Thank you so much

Reply