In a page.template, I'm trying to display a separate line for every product I have in the cart, meaning that if the same product is present, in example, 3 times, I need to show it in 3 separate lines.
The basic structure is this:
{% for item in cart.items %}
{% for quantity in item.quantity %}
<p>show something</p>
{% endfor %}
{% endfor %}
But that <p>is not showing.
This sintax is working:
{% for item in cart.items %}
{% for i in (1..item.quantity) %}
<p>show something</p>
{% endfor %}
{% endfor %}
(snap...! auto high-five)
Related
I wrote the following code:
{% extends "base.html" %}
{% block content %}
{% if all_site %}
<ul>
<h3>Here all my site:</h3>
{% for site in all_site %}
<li> {{ site.name }}</li>
{% endfor %}
</ul>
{% else %}
<h3> You don't have any Site.</h3>
{% endif %}
{% endblock content %}
when, I run. I not see "Here all my site", but I only see the one contained in the for.
I tried to modify it, for now a solution that I have found working is this:
{% extends "base.html" %}
{% block content %}
{% if all_site %}
<h3>mmmmmm</h3>
<ul>
<h3>Here all my site:</h3>
{% for site in all_site %}
<li> {{ site.name }}</li>
{% endfor %}
</ul>
{% else %}
<h3> You don't have any Site.</h3>
{% endif %}
{% endblock content %}
in this case if I do run, i see:
here all my site:
site 1
site 2
{% extends 'base.html' %}
{% block content %}
<p>Welcome to home page.</p>
<p>{% user.groups.all() %}</p>
{% endblock %}
At the moment I'm trying to figure out how I could even get all the user's groups to show on the page. This results in an error.... Invalid block tag on line 5: 'user.groups.all()', expected 'endblock'. Did you forget to register or load this tag?
I have tried to do if statements, but it seems break as soon as it meets one condition. For example if user is a part of test1 and test2 groups, I'd like for it to display test1 and test2, but it only displays test1.
{% extends 'base.html' %}
{% block content %}
<p>Welcome to home page.</p>
{% if user.groups.all.0.name == "test1" %}
<p>test1</p>
{% if user.groups.all.0.name == "test2" %}
<p>test2</p>
{% endif %}
{% endblock %}
In the first code, you should have used {{ }} instead. In order to access groups, do this:
{{ user.groups.all }}
and to check for a specific group:
{% if desired_group in user.groups.all %}
some html..
{% endif %}
if you want to output specific html for each group:
{% for group in user.groups.all %}
{% if group == desired_group %}
some html..
{% endif %}
..some more conditions..
{% endfor %}
I am trying to display a list of all articles using liquid markup. I've got this code which displays them properly, however I want to be able to sort by the modified date descending (most recent article on top). How can this be accomplished?
I was thinking that perhaps I need to create a new array with all articles in it and then sort that, but I am not sure how to do that. Also note that I want to sort ALL of my articles by date, not just within each folder.
{% for category in portal.solution_categories %}
{% if category.folders_count > 0 %}
{% for folder in category.folders %}
{% for article in folder.articles %}
{{ article.title }} - {{ article.modified_on | short_day_with_time }} <br>
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
Thanks!
You can use a variable to sort the list of articles and then iterate that variable.
{% for category in portal.solution_categories %}
{% if category.folders_count > 0 %}
{% for folder in category.folders %}
{% assign sorted = (folder.articles | sort:date) %}
{% for article in sorted %}
{{ article.title }} - {{ article.modified_on | short_day_with_time }} <br>
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
In shopify I am using liquid templating to get blog posts which are related to products by their tags, like so:
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% endif %}
{% endfor %}
</ul>
However, if there are no related posts, I would like to display a message such as "No related posts!"
My question is how would I do that? I have contemplated trying to get the articles into an array and testing if it is empty, but I am having difficulty finding out how this is done.
Thanks!
Try something like this:
{% assign related_posts = "" %}
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
{% capture post %}
<li><p>{{ article.title }}</p></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% else %}
<li>No related blog posts!</li>
{% endif %}
{% endfor %}
</ul>
I posted a question, the other day, of how to see if an item in the cart had a specific tag, and got my answer:
{% for item in cart.items %}
{% if item.product.tags contains 'SampleProduct' %}
<p>Enjoy your Product</p>
{% endif %}
{% endfor %}
This code worked. But I would like to combine it with code that checks if a product in the cart has a certain number of products. So, something like- a customer would have to buy 50 of a product to check out, and any less they would get an error message. Here is the code I have that checks if the customer is purchasing enough items:
{% if cart.total_amount <= 49 %}
<p>You need to purchase 50 items or more to checkout</p>
{% else %}
{{include check out button}}
{% endif %}
So I tried to use both of these together like this:
{% for item in cart.items %}
{% if item.product.tags contains 'SampleTag' %}
{%for cart.items %}
{% if product.item.total_amount <= 49 %}
<p> You will need to purchase more than 50 of this product </p>
{% else %}
*{{ Include Checkout button }}
{% endif %}
{% endfor %}
{% else %}
*{{ Include Checkout button }}
{% endif %}
{% endfor %}
*First: I don't know if I need two check out buttons
Second: When I use this code I get an error saying: "Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]"
Third: Will this loop though the products in my cart or just group them together? I would like it to loop though and check each product set individually.
Can any one help me out? Why am I getting the Syntax Error? Is there any thing else I can do to improve my code?
Thanks for any help
To answer your questions:
You should only need one checkout button, see my code below.
Your inner for loop should be {% for item in cart.items %}, not {%for cart.items %}
See code below.
Try something like this:
{% assign show_checkout_button = true %}
{% for item in cart.items %}
{% if item.product.tags contains 'SampleTag' and item.quantity <= 49 %}
{% assign show_checkout_button = false %}
<p>You will need to purchase more than 50 of the product "{{ item.product.title }}"</p>
{% endif %}
{% endfor %}
{% if show_checkout_button %}
<!-- show checkout button -->
{% endif %}
The above code loops through each product in the cart, and if the product has the tag "SampleTag" and a quantity less than 50 it shows an error message instead of the checkout button.