IF statement that shows a different text according to the shopping cart amount - shopping-cart

I need to make an IF statement script on my Shopping Cart, which displays a different text depending on the amount in the basket. Currently I only have two options. IF/ELSE. How do I make several IF statements which changes on a specific amount?
So far I've made this, which only applies to an amount below/beyond 1000:
{% assign totalCart = 0 %}
{% for item in cart.items %}
{% assign totalCart = totalCart | plus:item.totalCart_raw %}
{% endfor %}
{% comment %}
If the amount
{% endcomment %}
{% if totalCart >= 1000 %}
Volume Discount 20%
{%else%}
Volume Discount available 30%
{%endif%}

Example of using if + elsif (based on the code in the question)
{% assign totalCart = 0 %}
{% for item in cart.items %}
{% assign totalCart = totalCart | plus:item.totalCart_raw %}
{% endfor %}
{% comment %}
If the amount
{% endcomment %}
{% if totalCart < 1000 %}
Volume Discount 20% use this code
{% elsif totalCart < 2000 %}
Volume Discount available 30% use this code
{% elsif totalCart < 3000 %}
Volume Discount available 40% use this code
{% elsif totalCart < 4000 %}
Volume Discount available 50% use this code
{% elsif totalCart > 4000 %}
Volume Discount available 60% use this code
{% endif%}

Related

Flaks / Jinja nested if statement

I am stuck on a nested if statement, thinking I am approaching it wrong. I have a list of about 100 products that I am rendering on my page. I want to have a nested 'if' statement. Users will have a toolkit, and if the product is in their toolkit, i want to render "Already Uses", else "Add to toolkit?"
{% for product in products|sort(attribute="name") %}
{{ product.name }}
{% for products in toolkit %}
{% if product.name in toolkit %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}
{% endfor %}
more details on: jinja2 check if value exists in list of dictionaries
You are spot on and on the right path. It would help to provide the structure (as an example) of the product object ...
{% for product in products|sort(attribute="name") %}
{{ product.name }}
{% if product.name in toolkit %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}
{% endfor %}
If toolkit is a nested map map:
{% if product.name in toolkit|map(attribute="<whatever has the value equal with product.name>") %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}

How to do an if statement in a Django template to find if a decimal value is 0

Basically, I want to find out if a decimal field value is 0.00. Then, I want to output a different value in the template. See code below. The code does not work.
variable = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
#template
{% for item in table %}
{% if item.variable is 0.00 %}
<li><strong>Total: </strong> Unknown </li>
{% else %}
<li><strong>Total:</strong> ${{ item.variable }}</li>
{% endif %}
{% endfor %}
This does not work. It outputs: Total: $0.00.
You can check this with if not item.variable for example, since 0.00 has truthiness False. This condition will also check if the value is None (so NULL in the database), which is probably something you should consider as well:
{% for item in table %}
{% if not item.variable %}
<li><strong>Total: </strong> Unknown </li>
{% else %}
<li><strong>Total:</strong> ${{ item.variable }}</li>
{% endif %}
{% endfor %}
If you only want to check for zero, you can check it with == 0:
{% for item in table %}
{% if item.variable == 0 %}
<li><strong>Total: </strong> Unknown </li>
{% else %}
<li><strong>Total:</strong> ${{ item.variable }}</li>
{% endif %}
{% endfor %}

Examine cart in Shopify

I'm hoping for some help with liquid in Shopify, I'm having trouble with trying to customise the cart based on what product the customer has added.
Basically if a customer adds product from Vendor A then I want the cart to load the template to customise the cart for Vendor A
But if product is from Vendor B then I want it to load the template to customise the cart for Vendor B
But if the cart has products from neither (or both) then I want it to load the default cart.
{Edit: I worked out what was wrong with my code to load the templates but now I just need help with the logic so that when the cart has products from both brands it loads the default cart. Because at the moment it loads both cart snippets into the page}
Any help massively appreciated!
{% for item in cart.items %}
{% if item.vendor == 'Brand A' %}
{% include 'cart-a' %}
{% elsif item.vendor == 'Brand B' %}
{% include 'cart-b' %}
{% else %}
{% section 'cart-default %}
{% endif %}
{% endfor %}
also tried this:
{% case cart.items %}
{% when item.vendor == 'Brand A' %}
{% include 'cart-a' %}
{% when item.vendor == 'Brand B' %}
{% include 'cart-b' %}
{% when item.vendor == ‘Brand A’ and item.vendor == 'Brand B' %}
{% section 'cart-default' %}
{% else %}
{% section 'cart-default' %}
{% endcase %}
I think this steps may help you...
Step 1: Create different section instead of snippets for two different types of vendors and default
Step 2: Follow the below code in cart.liquid
{% assign vendor = '' %}
{% assign same = true %}
{% for item in cart.items %}
{% if vendor != '' or vendor == item.vendor %}
{% assign vendor = item.vendor %}
{% else%}
{% assign same = false %}
{% endif %}
{% endfor %}
{% if same == true %}
{% if vendor == 'Brand A' %}
{% section 'cart-a' %}
{% elsif vendor == 'Brand B'%}
{% section 'cart-b' %}
{% else %}
{% section 'cart-default' %}
{% endif %}
{% else %}
{% section 'cart-default' %}
{% endif %}
In liquid it is easier to work with arrays. Working code for you:
{% assign vendors = cart.items | map: 'vendor'| uniq | join: ' ' %}
{% if vendors contains "Brand A" and vendors contains "Brand B" %}
{% section 'cart-default' %}
{% else %}
{% if vendors contains "Brand A" %}
{% section 'cart-a' %}
{% else %}
{% if vendors contains "Brand B" %}
{% section 'cart-b' %}
{% else %}
{% section 'cart-default' %}
{% endif %}
{% endif %}
{% endif %}

Multiple condition When statement in Big Cartel

I am trying to make a multiple condition statement on Big Cartel and I keep getting an error telling me that there is an unknown tag in my When statement. I feel like this is very simple. What am I doing wrong? Thank you for looking.
{% case product.status %}
{% when 'sold-out' %}
{% if product.id = '25027747' %}
RESULT 1
{% else %}
{% if product.id = '25027993' %}
RESULT 2
{% else %}
{% endif %}
You'll want to use this code:
{% case product.status %}
{% when 'sold-out' %}
{% if product.id = '25027747' %}
RESULT 1
{% elsif product.id = '25027993' %}
RESULT 2
{% endif %}
{% endcase %}

Django {% with %} tags within {% if %} {% else %} tags?

So I want to do something like follows:
{% if age > 18 %}
{% with patient as p %}
{% else %}
{% with patient.parent as p %}
...
{% endwith %}
{% endif %}
But Django is telling me that I need another {% endwith %} tag. Is there any way to rearrange the withs to make this work, or is the syntactic analyzer purposefully carefree in regards to this sort of thing?
Maybe I'm going about this the wrong way. Is there some sort of best practice when it comes to something like this?
if you want to stay DRY, use an include.
{% if foo %}
{% with a as b %}
{% include "snipet.html" %}
{% endwith %}
{% else %}
{% with bar as b %}
{% include "snipet.html" %}
{% endwith %}
{% endif %}
or, even better would be to write a method on the model that encapsulates the core logic:
def Patient(models.Model):
....
def get_legally_responsible_party(self):
if self.age > 18:
return self
else:
return self.parent
Then in the template:
{% with patient.get_legally_responsible_party as p %}
Do html stuff
{% endwith %}
Then in the future, if the logic for who is legally responsible changes you have a single place to change the logic -- far more DRY than having to change if statements in a dozen templates.
Like this:
{% if age > 18 %}
{% with patient as p %}
<my html here>
{% endwith %}
{% else %}
{% with patient.parent as p %}
<my html here>
{% endwith %}
{% endif %}
If the html is too big and you don't want to repeat it, then the logic would better be placed in the view. You set this variable and pass it to the template's context:
p = (age > 18 && patient) or patient.parent
and then just use {{ p }} in the template.