Adding to liquid assign variable

Highlighted
Shopify Partner
221 1 14

I was wondering, I saw a post from a while ago that assign variables were only string variables. Is this still true today? I was trying to add to a created assign variable as in my next code: 

{% for line_item in order.line_items %}
    {% assign otro = 0 %}
    {% assign panales = 0 %}
    {% assign formula = 0 %}
    // Product Type = {{ line_item.type }} | Product Price = {{ line_item.price }}
    {% if line_item.type == "Pañales" %}
      {% assign panales |  plus line_item.price %}
    {% elsif line_item.type == "Formula" %}
      {% assign formula |  plus line_item.price %}
    {% else %}
      {% assign otro |  plus line_item.price %}
    {% endif %}
  {% endfor %}

I was hopeing to do this via liquid, but if its not possible Ill make it work using javascript hehe. I just wanted to know.

Also for some reason I cant seem to get the print on the line_item.type. Am I making something wrong there? This is for the thankyoupage.

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Astronaut
1890 0 385

You can assign strings, numbers/values, and even arrays to Liquid variables.

What are you trying to do with that section of code?

0 Likes
Shopify Partner
221 1 14

Im trying to sum the total amount spent per category type. So to get how much was spent on daipers = Pañales, Formulas = formulas and other category types = otro. So the sum of this three should add up to the toal amount of the purchase. Note this is on the Thank You Page. What I got so far is this: 

{% assign otro = 0 %}
  {% assign panales = 0 %}
  {% assign formula = 0 %}
  {% for line_item in order.line_items %}
    // Product Type = {{ line_item.type }} | Product Price = {{ line_item.price | times: line_item.quantity | money_without_currency | remove: ',' }}
    {% if line_item.type == "Pañales" %}
      {% assign pricepanales = 0 | plus: line_item.price | times: line_item.quantity %}
      {% assign panales = panales | plus: pricepanales %}
    {% elsif line_item.type == "Formula" %}
      {% assign priceformulas = 0 | plus: line_item.price | times: line_item.quantity %}
      {% assign formula = formula | plus: priceformulas %}
    {% else %}
      {% assign priceotro = 0 | plus: line_item.price | times: line_item.quantity %}
      {% assign otro = otro | plus: priceotro %}
    {% endif %}
  {% endfor %}

I noticed I had the assign inside the for shouldnt go there. Also added a variable to get the total amount per product and then sum it to the collective I have of each category type. Im doing this to send this to our analytics and see how much is spent on daipers and formulas.

This solved my problem :D I get the total amount in the sum on the otro categtory, but Im still in trouble with the line_item.type. For some reason I cant print the product type. It does no bring anything back D: Am I doing something wrong with the name of the variable, or maybe this variable cant be called on the thank you page? Without this I cant know if the product is from daipers or formulas D: It all goes to other even if they are from the daipers category.

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Astronaut
1890 0 385

You'll want to use line_item.product.type .

    {% if line_item.product.type == "Pañales" %}
      ...
    {% elsif line_item.product.type == "Formula" %}

By the way, if you want to shorten the code a bit, you can make use of line_item.line_price.

      {% assign panales = panales | plus: line_item.line_price %}

 

1 Like
Shopify Partner
221 1 14

Dude You are a genious! :D Thanks so much for the assist!! Here is the finished code if someone else stumbles upon this same problem hehe:

{% assign otro = 0 %}
  {% assign panales = 0 %}
  {% assign formula = 0 %}
  {% for line_item in order.line_items %}
    // Product Type = {{ line_item.product.type }} | Product Price = {{ line_item.price | times: line_item.quantity | money_without_currency | remove: ',' }}
    {% if line_item.product.type == "Pañales" %}
      {% assign panales = panales | plus: line_item.line_price %}
    {% elsif line_item.product.type == "Formula" %}
      {% assign formula = formula | plus: line_item.line_price %}
    {% else %}
      {% assign otro = otro | plus: line_item.line_price %}
    {% endif %}
  {% endfor %}

 

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes