PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, June 30, 2022

[FIXED] How to use filters inside an if condition? - shopify liquid

 June 30, 2022     filter, if-statement, liquid, shopify     No comments   

Issue

Is it possible to use filters inside an if statement condition ?

I found no way of doing this unless I make a variable that stores the filtered data and then use it inside my condition. Seems odd to me, there must be a better way.

I want to do something like this but I am getting an error:

{% if numA | plus:5 >= numB %}

I want to avoid doing this:

{% assign temp = numA | plus:5 %}
{% if temp >= numB %}

Solution

What you are trying to do in not possible in Shopify Liquid. From the official Shopify Liquid issues page

Parenthesis aren't allowed in Liquid. They can't be used in conditionals the way you would use them in a programming language.

Filters are not allowed in conditionals, they will lead to unexpected results, at least in Shopify.

The following:

{% if cart.item_count|times:1 > 5 %}

Generates this Liquid warning:

Expected end_of_string but found pipe in "cart.item_count|times:1 > 5"

So, the only possible solution is what you suggested in your own question.

{% assign temp = numA | plus:5 %}
{% if temp >= numB %}

Math filters in IF condition - Liquid



Answered By - Bilal Akbar
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing