How to get all addresses - javascript

Currently I'm setting value to display list address as 200, but it is not an optimal way. How can I get all the addresses?
{% paginate customer.addresses by 200 %}
I tried, but it doesn't work
% paginate customer.addresses by customer.all_addresses_count %}

Related

flask javascript can't print/log jinja2 request.form variable

Hi all I'm trying to get the value from flask and print it in javascript.
When I do print with a javascript string variable in my form it prints none.
var javascript_variable = "key_name";
// output is: request
console.log('request'+"{{request.form['"+javascript_variable+"']}}");
Also tried
// output is: request
console.log('request'+"{{request.form["+javascript_variable+"]}}");
however when I manually type the key name it works.
// output is: request key_information
console.log('request'+"{{request.form['key_name']}}");
Can someone please tell me what I'm doing wrong.
EDIT: I do not want to hardcode the keyname because the key_name is a changing dynamically
I'm trying to access that variable, so I can populate my select option/checkbox/radiobox option something like <option {% if request.form['key_name'] == "value" %} selected {% endif %} same for checkbox and radio box
In order to do that, you can use the flash statements inside the views in flask as follows,
#blueprint.route('/something.html', methods=['GET', 'POST'])
#login_required
def function():
try:
flash("Submission successful!")
except Exception as e:
traceback.print_exc()
return render_template('something.html')
In jinja, you can access the flash variable in the following way,
{% if get_flashed_messages() %}
{% for message in get_flashed_messages() %}
<script>window.alert("{{ message }}")</script>
{% endfor %}
{% endif %}

Avoid pagination limits for simple output - Shopify / Liquid

I want to create a simple list of every article URL in my blog (I have >150).
My current code:
{% for article in blogs['myblog'].articles %}
{{article.url}}
{% endfor %}
The problem is that it only outputs 50 URLs, as per the standard Shopify pagination limit. How could I overcome this? Any solutions using Ajax or otherwise would be highly appreciated.
You can overwrite the pagination limit simply by wrapping the call with the pagination tag and passing the number you want to paginate.
{% paginate blogs['myblog'].articles by 999 %}
{% for article in blogs['myblog'].articles %}
{{article.url}}
{% endfor %}
{% endpaginate %}
PS: Please note that the more articles you have the longer it will take for the DOM to load.

I need to get Shopify to change the product description when you change the variant. Is there a JavaScript function?

I am sure every Shopify newbie eventually gets to this point. On the Product page I need the Description to change when the Variant changes. In other words, if the customer chooses "A - Grade" from the Condition pull-down I need the Description to change to a Grade A Description. Same with grade B and C.
if customer chooses Grade A
change product description
I achieved this already by putting an IF statement in the Shopify->Sections->product-template.liquid. However, this solution requires a screen refresh which does not look good to the customer. Makes the page look broken.
{% if product.selected_variant.option1 == "A - Grade" %}
<B>A-GRADE</B>: Very light use, if any, ...
{% endif %}
{% if product.selected_variant.option1 == "B - Grade" %}
<B>B-GRADE</B>: Normal use, no dings or dents...
{% endif %}
{% if product.selected_variant.option1 == "C - Grade" %}
<B>C-GRADE</B>: Medium to heavy use, might ...
{% endif %}
Again, this requires a page-refresh. Even if you code the refresh to happen automatically it still looks bad especially if the customer is on a slower connection.
There is already some code in the themes.js that changes the price and image when you select a new varriant:
this.$container.on('variantChange' + this.settings.namespace, this._updateAddToCart.bind(this));
this.$container.on('variantImageChange' + this.settings.namespace, this._updateImages.bind(this));
this.$container.on('variantPriceChange' + this.settings.namespace, this._updatePrice.bind(this));
this.$container.on('variantSKUChange' + this.settings.namespace, this._updateSKU.bind(this));
I do not know any javascript so I am lost. Can't we just copy the existing variantPriceChange function and change it into a variantDescriptionChange?

Template script to make it visible only to admin on shopify

I would like to make a template on shopify visible only to admins.
I can make it visible to customers only, but need a code for admin only now.
The code for customers I use is:
{% unless customer %}
{% if template contains 'customer' %}
{% assign send_to_login = false %}
{% else %}
{% assign send_to_login = true %}
{% endif %}
{% endunless %}
{% if send_to_login %}
<meta content="0; url=/account/login?checkout_url={{ shop.url }}" http-equiv="refresh" />
{% else %}
==== Page Content ===
{% endif %}
//The code above asks to the user to login if they visit this template
Now is it possible to make something similar to this for the admins only? What is the name for the admin entity? Here for example the entity customer is 'customer'. How should I declare the admin in the code?
Thanks in advance.
See these similar questions:
How to check if current visitor is shop's admin
Check if current customer is an admin in Shopify?
Check if current customer/user is an admin on product or collection pages?
Currently there's no nice way to check for admins like you can for customers. Your options are to tag your customers as admins and only show your template if the customer has the admin tag, or check against the customer's email address to determine if they are an admin.

Django template ifequal statement always true

Hi I am building a simple blog using Python/Django. In my index.html file, I am trying to show archived posts when a button containing a month is clicked. At the moment, I am just trying to get the id of every post made in that month into a javascript array. For some reason though,it always returns true!
<script type="text/javascript">
function showPosts(month)
{
var posts_in_month=[];
{% for post in all_posts %}
var match = {{ post.pub_date.month }}
{% ifequal match month %}
posts_in_month.push({{ post.id }})
{% endifequal %}
{% endfor %}
}
</script>
I then go on to have a switch statement where I show the contents of the array depending on the month clicked. The code definitely works, when I call an alert(month) it shows up correctly, and when I call alert({{ post.pub_date.month }}) it shows up fine as well. These are usually not the same though, why is it always evaluating the ifequal to true?
You cannot create Javascript variable in python and use it as python's variable.
Replace the match with actual value:
{% ifequal post.pub_date.month month %}
posts_in_month.push({{ post.id }})
{% endifequal %}

Categories