Shopify getJSON('/cart.js') custom values of liquids - javascript

Good time of day.
Trying to improve the pop-up cart was stuck on the possibility of using custom values instead of existing ones.
This example works as expected:
$.getJSON('/cart.js').then(
function(cart) {
$('#example').html(theme.Currency.formatMoney(cart.total_price, theme.moneyFormat));
});
In the cart template, I have made other values by liquid codes. Here is an example:
{% for item in cart.items %}
{% assign totalfive = 0 %}
{% assign fiveminus = cart.total_price | times: 0.05 %}
{% assign totalfive = cart.total_price | minus: fiveminus %}
{% endfor %}
In HTML, this value looks like this: <span>{{ totalfive | money }}</span>
The question is about how can I get this value by JSON? Maybe there is a way to catch it with an element based on ID? Or it definitely impossible?

Related

HTML if else condition for printing items in list

I'm making a website with flask, and I pass in a list. My .html file has this code:
{$ for file in list %}
<div>
<img ... > </img>
</div>
{% endfor %}
Now if list.length == 0 I want it to just do something like
<h2> Error </h2>
instead. How do I make an if statement to check if the list is empty, and if it is then I print error on the website, if it is not, I print the images in the list. I tried to put:
<script>
if(list.length==0){
<h2>Error</h2>
}
else{
"the for loop stuff"
}
</script>
but this does not work.
THanks
In Flask with Jinja2 as a template engine, the if-else syntax requires you to write the following statement
{% if mylist.length == 0 %}
<div>error</div>
{% else %}
<!-- loop -->
{% endif %}
Note: Since list is a Python keyword i suggest you to use another name for your list in order to avoid possible conflicts.
In this pythonprogramming page about flask 7, you will find some example code and a video. The example shows an if-else statement:
{% for post in posts %}
<h3> {{ post.title }}</h3>
<p>{{ post.body }}</p>
{% if author %}
<span style="font-size:0.5em;color:yellowgreen">{{ post.author }} </span>
{% endif %}
{% if date %}
<span style="font-size:0.5em;color:yellowgreen">{{ post.date }}</span>
{% endif %}
{% endfor %}
I decided to post my answer to share this learning resource because I have noted that the example does not use any <script> tag (not sure if it helps).
The comparison operator is almost the same everywhere as you can see in both the Python and Jinja references.
Therefore, the equivalent for your code would be:
{% if list.length == 0 %}
...
{% endif %}

Create variable in Django template

I have this in my template:
{% for category in menu_categories %}
{% with menu_button = "menu_"{{category}} %}
<button class="button {{menu_button}}" onclick="showItem(menu_button)">{{category}}</button>
{% endwith %}
{% endfor %}
I am trying to create a series of buttons with class name by iterating a queryset of model categories.
I create a variable menu_button so that I could systematically name them, and pass the name to a JavaScript function showItem().
But I get the error 'with' expected at least one variable assignment. What am I doing wrong? Thanks.

What does the vertical bar mean in Flask Templates?

I am working on a flask tutorial, and in the sample code, it contains the following:
| {% for pr in providers %}
{{ pr.name }} |
{% endfor %}
In this context, what do the vertical bars mean?
| {% for pr in providers %}
{{ pr.name }} |
{% endfor %}
this bars will be displayed in the html output, it is there to enhance readability. For example, if the providers list contains say 3 links in total they will be displayed one after another separated by | due to for loop.
possible output:
|link1|
link2|
link3|

Shopify - Variant quantity in my cart liquid cart.js

I've been spending hours to try to figure it out + forum, so next step is to post my problem! :)
So what I want to do is to display a message to my user in my cart.js. They are allowed to order when the product is "out of stock" but I want to display a message on my cart.js saying 2/3 weeks delivery.
I tried to add that in my cart.liquid :
bkseen
<p class="cart-item__variant"> {{ line_item.variant.inventory_quantity }}</p>
{% assign lineItemVariant = line_item.variant %}
{% if lineItemVariant.inventory_management == "shopify" %}
<span class="delivery-message">
{% if lineItemVariant.inventory_quantity > 0 %}
Quick delivery
{% else %}
2-3 weeks delivery
{% endif %}
</span>
{% endif %}
I've tested the code above and the result is pretty weird.
If I put the quantity in my table between <td></td>, that automatically format every values as the last one. I guess it's because of Rivet.js mixing data on the back.
And if I put the code out of <td> tags it works perfectly.. the thing is, I want to display it for each line, that's the point !
Check the result by a screenshot :
HERE
If you have any question do not hesistate.
Cheers
bkseen

Shopify - Load 'collections/all' page on my front page

How to load http://example.myshopify.com/collections/all content
to my shopify frontpage http://example.myshopify.com/
I figured out a way that I hardcode <script>window.location.href='collections/all'</script> on index.liquid, but I'm pretty sure thats' not a clean way.
and I try to copy whole collection.liquid's code to index.liquid, but it prompt me Liquid error: Array 'collection.all.products' is not paginateable. error and no product showing the index.liquid page.
Any idea how to load collections/all on shopify's front page?
I'm using Timber Framework as people recommend to start to build a theme
inside
In the Timber framework, you could change this line in index.liquid:
{% for product in collections.frontpage.products limit:4 %}
to:
{% for product in collections.all.products %}
Depending on how many products you have, you probably still want to limit how many are displayed, or paginate the output.
E.g.
{% paginate collections.all.products by 12 %}
{% for product in collections.all.products %}
...
{% endfor %}
{{ paginate | default_pagination }}
{% endpaginate %}
You include this:
{% for product in collections.all.products %}
// here you have access to each product
{% endfor %}
This will loop all of your products.
You can review http://cheat.markdunkley.com/ what product variables you have access to in that loop.

Categories

Resources