Liquid filter "plus: 0" not working for strings

New Member
5 0 0

Hello,

For the past couple of years, we have been using the "plus: 0" liquid filter to convert strings to integers. This is what I was taught by the person who was handling the code before me, and it's what I found in several forums for comparing strings to integers. The code looks as follows:

{% assign product_price = product.metafields.special_price | remove: "$" | replace: ".","" | plus: 0 %}

We have had to revise the code several times in the last month, because since November, different parts have stopped working. I have suspected changes on Shopify's end, since no changes were made on our end, yet the code keeps breaking. Now, the plus: 0 portion has stopped working. My troubleshooting, this time around, went like so:

{{ product.metafields.special_price }}  returns $69.99

{{ product.metafields.special_price | remove: "$" }}  returns 69.99

{{ product.metafields.special_price | remove: "$" | replace: ".","" }}  returns 6999

{{ product.metafields.special_price | remove: "$" | replace: ".","" | plus: 0 }}  returns 0

This is actually causing a problem with some other pieces of code, as well. As I said, this just started recently (within the last week, I'd guess, since I literally had it working around Wednesday, December 7), and we have been using this method for the past couple of years.

Any insight would be appreciated.

0 Likes
Highlighted
Astronaut
1890 0 373

I believe a metafield should have a namespace and a key.  For example:

Metafield
  namespace: special_price
  key:       price
  value:     $69.99

Try this: 

{{ product.metafields.special_price.price | remove: "$" | replace: ".","" | plus: 0 }}

 

0 Likes
Shopify Expert
2000 82 1452

I don't see the same results here. For example:

{% assign string = '$69.99' %}
1: {{ string | remove: "$" }}
2: {{ string | remove: "$" | replace: ".","" }}
3: {{ string | remove: "$" | replace: ".","" | plus: 0 }}

Result:

1: 69.99
2: 6999
3: 6999

Are you able to show the value of the metafield when using the json filter?

{{ product.metafields.special_price | json }}

Edit: Alex could be on the money here too - I just assumed that was a typo but perhaps not.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
New Member
5 0 0

I apologize. I used "special_price" because I was still digging through my code and didn't consider that everything is important. Please forgive my mistake. The actual code appears as follows, where "special_price" was my shorthand for "shappify_csp[attribute_price]":

{% assign product_price = product.metafields.shappify_csp[attribute_price] | remove: "$" | replace: ".","" | plus: 0 %}

where [attribute_price] is assigned a value based on Customer tags and functions as the key. We are running Bold Apps' Customer Pricing app for multiple price points.

Also, I don't know anything about using the json filter. I'm really quite new to liquid, and most of what I know has been figure-it-out-as-I-go.

Jason, I used to get the same results as you, up until yesterday, when suddenly it wasn't working at the plus:0 filter. Everything else returns properly. I also have the following code giving me the same problems:

{% if collection.description contains '[old_price_a]' %}         
          {% assign old_price_a_first = collection.description | split:'[/old_price_a]' | first %}
          {% assign old_price_a = old_price_a_first | split:'[old_price_a]' | last | plus:0 %}
{% endif %}

which is pulling from the following code in the description:

<!--[old_price_a]6999[/old_price_a][new_price_a]1699[/new_price_a]-->

Ultimately, the product_price variable gets comapred to old_price_a to determine if new_price_a should be displayed:

{% if product_price == old_price_a %}
          				{{new_price_a | money}}
{% endif %}

However, both pieces stop working at the plus:0 filter. I get appropriate returns up until that point. After the plus:0, I get a value of "0" for both product_price and old_price_a.

0 Likes
New Member
5 0 0

I just tried the json filter. I'm not sure if I'm doing it right:

{% assign json_price = product.metafields.shappify_csp[attribute_price] | json %}
{{ json_price }}

Returns:

"\u003cspan class=money\u003e$69.99\u003c\/span\u003e"

Is that what it's supposed to look like?

0 Likes