Why am I getting a liquid variable assignment error on my collection page?

Solved

Why am I getting a liquid variable assignment error on my collection page?

leungvi8
Shopify Partner
20 1 3

Hey there,

Just having this error on a snip section of my collection page:

    Liquid syntax error (sections/collection-template.liquid line 2): Expected end_of_string but found id in "{{product.compare_at_price and product.compare_at_price != product.price }}"
    Code: 
{% for product in collection.products %}
        {% assign sale = product.compare_at_price and product.compare_at_price != product.price %}
        {% assign sold_out = product.available %}
        <li class="card " style="position relative; padding:10px">
            {% if product.compare_at_price and product.compare_at_price != product.price %}
                <div class="position_absolute--mix product_ribbon--style product_sale--style text-center fw-bold" style="top:0;right:0;">SALE</div>
            {% else if product.available == false %}
                sold out
            {%endif%}
            <a href="{{ product.url | within: collection }}">
                <img class="card-img-top" src="{{ product.featured_image.src | img_url: 'x400' }}" alt="{{ product.featured_image.alt | escape }}">
            </a>
            <div class="card-body text-center">
            <a class="card-title" href="{{ product.url | within: collection }}"><p>{{ product.title }}</p></a>
            <h5 class="fw-semibold">{{ product.price | money }}</h5>
            </div>
            <form id="form-{{product_item.handle}}" method="post" action="/cart/add">
                <input type="hidden" name="id" value="{{product_item.variants.first.id}}">
                <a onclick="document.getElementById('form-{{product_item.handle}}').submit()" class="btn primary_btn--style" >ADD</a>
            </form>
            {% unless product.available %}<br><strong>sold out</strong>{% endunless %}
        </li>
  {% else %}
    <p>no matches</p>

  {% endfor %}

 

I'm trying assign this variable as a conditional statement. Am I missing something? Thanks!

Accepted Solution (1)

iDoThemes
Trailblazer
207 43 93

This is an accepted solution.

You can't assign a variable as a condition like this.  You'll need to initialise it first, then reassign it inside an if statement.

 

{% assign var = false %}
{% if condition %}
  {% assign var = true %}
{% else %}
  {% assign var = false %}
{% endif %}

 

 

Developer of Liquify Chrome Extension -- Enhance the Shopify Theme Code Editor
.




Theme Developer -- Drop me a line

View solution in original post

Replies 3 (3)

iDoThemes
Trailblazer
207 43 93

This is an accepted solution.

You can't assign a variable as a condition like this.  You'll need to initialise it first, then reassign it inside an if statement.

 

{% assign var = false %}
{% if condition %}
  {% assign var = true %}
{% else %}
  {% assign var = false %}
{% endif %}

 

 

Developer of Liquify Chrome Extension -- Enhance the Shopify Theme Code Editor
.




Theme Developer -- Drop me a line
leungvi8
Shopify Partner
20 1 3

Oh okay awesome thanks! I was just following that from a Udemy Course too... but i guess maybe that was outdated code?

Thanks for the help.

lucaswang5277
Visitor
2 0 0

Hi,

I have met the same problem after I installed a new theme ella newest vision.

 

Liquid syntax error (collection-template-default line 15): Expected end_of_string but found id in "{{optionproduct-title change-text}}" 

 

It shows on my every collection page and cannot show the products correctly, but it is fine on my home page.

 

Could you please tell me how to fix it?

 

Thank you so much.

Best,

Lucas