How to access key name in json_string type of Shopify? - shopify

For example I have this JSON string in product_supplier['shipping_to']:
{"United States": {"time": "1-3"}, "Worldwide": {"time": "10-15"}}
How can I access the country name in liquid? Tried this:
{% for country, time in product_supplier['shipping_to'] %}
{{ country }}: {{ time['time'] }}
{% endfor %}
Which obviously doesn't work as it gives error: Liquid syntax error: Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]
And tried this:
{% for country in product_supplier['shipping_to'] %}
{{ country[0] }}, {{ country[1] }}
{% endfor %}
Which gives empty output with just a comma:
,
From the official doc here, it seems it's only able to access the values not keys?

If you saved the metafield as json_string then you can do this.
{% for item in product.metafields.product_supplier.shipping_to %}
{{item[0]}}<br/>
{% endfor %}
The response will be:
United States
Worldwide
Once again this must be as json string to work.
So it similar to your third example but we are using the proper object (in this case product.metafields) to target that metafield.
Tested it on my end it works without a problem.

Related

Django template syntax error: could not parse remainder % 2

I am getting a TemplateSyntaxError: "could not parse remainder % 2 from num%2":
{% if num%2 ==0 %}
{{"Even"}}
{% else %}
{{"Odd"}}
{% endif %}
You can't use arbitrary Python expressions in Django templates. You should create a custom filter for them.
However, for your expression there is a built-in tag divisibleby. From its example:
{{ value|divisibleby:"2" }}
If value is 4, the output would be True. So the final answer looks like (untested):
{% if num|divisibleby:"2" %}
Even
{% else %}
Odd
{% endif %}

How to get all the products starting with title 'A' in Shopify

Is there a way to get all the products starting with title A or B or C in Shopify without using collection.
Unfortunately this might be not possible.
Ideally this would work but it doesn't:
{% assign my_products = collections['all'].products | where: "title.first", "A" %}
{% for product in my_products %}
{{ product.title }}
{% endfor %}

Variable alias in a for cycle with Jinja2

I have this code inside my html:
{% for macchine in range(20) %}
{% set macchina_usata = 'M'+ macchine|string %}
{{ data['macchina_usata'] }}
{% if data['macchina_usata'] is defined %}
do something..
{% endif %}
{% endfor %}
before it was without the for cycle, I just had to check if a variable is defined and I got the result, but now I want to put it in a Cycle for because I have to check 20 or more variables.
The variables that I got from the previous html are like M1, M2, M3, ... M20 then I thought that was a good idea to create a varible macchina_usata composed by M+ the int macchine converted in a string, but when I try to print it nothing happen, so, i guess that I'm using the Alias in a wrong way
You're using the literal string 'macchina_usata' as an index for data. You should instead use variable macchina_usata, without the quotes:
{{ data[macchina_usata] }}
{% if data[macchina_usata] is defined %}

how to split the string in django template?

i am trying to split the string in template using custom template filter. But i got an error
TemplateSyntaxError at /job/16/
'for' statements should use the format 'for x in y': for skill in form.instance.skills | split : ","
Here it is my filter
#register.filter(name='split')
def split(value, key):
"""
Returns the value turned into a list.
"""
return value.split(key)
this is my template
<h4>Skills</h4>
{% for skill in form.instance.skills | split : "," %}
{{ skill }}
{% endfor %}
Thanks
Split is a custom filter, don't forget to create your filter, and to load it in your HTML page.
Documentation for Django 4.0: https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/
<h4>Skills</h4>
{% with form.instance.skills|split:"," as skills %}
{% for skill in skills %}
{{ skill }}<br>
{% endfor %}
{% endwith %}
For extract character string, use filter cut:
Phone
this removes the scripts from the string.
The direct for loop works too, you just have to remove the spaces in the syntax:
<h4>Skills</h4>
{% for skill in form.instance.skills|split:"," %}
{{ skill }}
{% endfor %}

Remove variants from variants array on product in Liquid

Good evening! I am trying to remove variants from the variant array on a product using pure Liquid(Shopify templating language). I would only like to use javascript as a last resort.
Below is where I am so far. Anything that is the variant in the if check needs to be removed from currentProduct.variants.
{% assign currentProduct = product %}
{% for variant in currentProduct.variants %}
{% include 'shappify-variant-is-csp' %}
{% if csp_variant != 1 %}
//need to remove the object that meets this if statement
{% endif %}
{% endfor %}
I'm pretty sure you're going to need to use some javascript to achieve this. Take a look at this article on the Shopify wiki: How do I remove sold out variants from my options drop-downs.
Modifying the code in that article for your situation, you'll want something like this:
{% for variant in product.variants %}
{% include 'shappify-variant-is-csp' %}
{% if csp_variant != 1 %}
jQuery('.single-option-selector option').filter(function() { return jQuery(this).html() === {{ variant.title | json }}; }).remove();
{% endif %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');