Shopify slice function returns '<' - shopify

I need to extract currency symbol in Shopify template. So far I've written:
{% assign symbol = product.price | money %} //creates a variable which holds price with currency symbol
{% assign symbol = symbol | slice: 0 %} //should return first char out of a string
{{ symbol }} //prints the variable
Unfortunately last line returns the < char.
Right now I'm out of ideas how to make this work. I know that Shopify can display currency by {{ shop.currency }} method, but it returns currency name instead of currency symbol.

Check the money format which is set in the Store Settings
Settings > General > Standards and formats > Currency > Change formatting
there are:
"HTML with currency"
"HTML without currency"
By default they are ${{amount}} USD and ${{amount}}, but they are wrapped them in span.money because you are using currency switcher.
<span class="money" >${{amount}} USD<span>
Easly you can use the filter strip_html to remove the span.money.
{% assign symbol = symbol | strip_html | slice: 0 %}

Related

Shopify Line item variants in separate lines

Currently in my shopify code I can use a line item input like so:
line_item.variant.title
This will output the following:
Snapback / One Size Fits All / Camo
What I'm trying to do is to break up each one into it's own line. So I can get this back:
Snapback
One Size Fits All
Camo
The challenge is that there are several products with different variants. Some contain the string "7/9" so I wouldn't be able to use "/" as a delimiter. Any suggestions?
The variant title is generated based on the variant options.
So if you like to show the different options you just call the options instead of the title.
Example:
{{ variant.option1 }}<br/>
{{ variant.option2 }}<br/>
{{ variant.option3 }}
Refer to the docs here: https://help.shopify.com/en/themes/liquid/objects/variant#variant-option1
I found this one is a better solution to set this dynamically:
{% if line.variant.title != 'Default Title' %}
<span class="order-list__item-variant variant-title">
{% assign variantOptions = line.variant.title | split: ' / ' %}
{% assign count = 0 %}
{% for option in line.product.options_with_values %}
<span><b>{{ option.name }} :</b> {{variantOptions[count]}}</span>
<br />
{% assign count = count | plus: 1 %}
{% endfor %}
<br />
</span>
{% endif %}
As by default, we are getting / in the value of line.variant.title. So we need to split this first So that we can get individual option values. and because there is no feasible object to get option label so we need to use the
line.product.options_with_values in a loop and iterate and set label with value as in the above code.
So, just use this code in your order confirmation email and you will get the format in the email as follow. Here Embroidery as yes and no. and Border as Zigzag and Simple are the options for product variants.

Liquid Warning: Unexpected character

To assign class depending on page category I have following in my code:
{% assign category_class = 'category-' | append: {{ page.category }} %}
As expected I get <div class="category-sometext". But when building I also get warning about an unexpected character in this line.
What's wrong and how I can fix it?
You need to remove the {{ }} around page.category as you are already inside {% %}. So:
{% assign category_class = 'category-' | append: page.category %}

Using a string to create a Liquid variable

In my shopify store, I am using SuperFields in order to customize my site, though my question isn't about the app. On one of my pages, I need the value for the following:
variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]
The value should be 0 or 1. If I evaluate the statement directly, such as:
{if variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key] =1%}
It throws an error when I render the page: Unexpected character '{'
I've also tried the following:
{% capture defaultImage %}variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]{% endcapture %}
{% assign test = defaultImage %}
But 'test' is considered nil and doesn't return any value. I have tried to search for answers here and on the shopify forum, but, as my clumsy post title suggests, I'm having a hard time concisely searching for a solution to this problem. Any help is greatly appreciated.
You can try :
{% assign metafield-key = collection.title | downcase | prepend: "sf_" %}
{% assign key = variant.metafields[metafield-key][meta_tag_key] %}
{% if key == 1 %}
Do the twist !
{% endif %}
You are missing a % sign in your code. Hence the error message. Your if statement started with {% and not just {
If you working in liquid then you have to use {% %} for defining any variable & also for condition in shopify. You can't use { this.

Liquid Script: Convert string to number in order to display as money

I'm trying to display a floating point value as money in liquid script. It appears to only allow you to do this to numbers, however I can't convert a string to a number (despite following advice on SO as below:)
{% assign teststring = '202.2400' %}
{% assign testnumeric = 202.2400 %}
teststring: {{ teststring }} <br/>
testnumeric: {{ testnumeric }} <br/>
teststring as money: {{ teststring |money: "GBP" }} <br/>
testnumeric as money: {{ testnumeric |money: "GBP" }} <br/>
{% assign testStringAsNumeric = teststring | plus: 45 %}
test convert teststring to numeric: {{ testStringAsNumeric }} <br/>
test show above as money: {{ testStringAsNumeric |money: "GBP" }}
The output is:
teststring: 202.2400
testnumeric: 202.24
teststring as money: 202.2400
testnumeric as money: £202.24
test convert teststring to numeric: 202.24000
test show above as money: 202.24000
What I want is to show the string value (teststring) as money. So I need to convert to a number and then display as money.
I've also tried the times filter e.g.
{% assign testStringAsNumeric = teststring | times: 1 %}
test show above as money: {{ testStringAsNumeric |money: "GBP" }}
But this returns an error:
test show above as money: System.Linq.Enumerable+d__112`1[System.String]
Thanks for any help
So there are a couple things with this question I'd like to address.
Firstly, when using the money filter, the number is interpreted as cents, eg.
Input: {{ 2000 | money }}
Output: $20.00
Because of this, your line below, once correctly formatted, will display $2.47.
test show above as money: {{ testStringAsNumeric |money: "GBP" }}
Second, the way to set which currency is going to be used by the money filter is inside Shopify admin, rather than inside the liquid drop as a parameter. Because of this, once you set your currency inside Shopify's admin, all you'll have to write is:
test show above as money: {{ testStringAsNumeric | money }}
Third, using the money filter, only two trailing zeroes will be kept.
Input: {{ 2040.002020203 | money }}
Ouput: {{ $20.40 }}
This is what I'm getting as output from your liquid code on my test store:
teststring: 202.2400
testnumeric: 202.24
teststring as money: Liquid error: wrong number of arguments (2 for 1)
testnumeric as money: Liquid error: wrong number of arguments (2 for 1)
test convert teststring to numeric: 247.24
test show above as money: Liquid error: wrong number of arguments (2 for 1)
Changing the | money: "GBP" filter to be simply | money (how Shopify normally uses the filter) gives:
teststring: 202.2400
testnumeric: 202.24
teststring as money: $2.02 !!
testnumeric as money: $2.02 !!
test convert teststring to numeric: 247.24
test show above as money: $2.47 !!
... which seems to be working as intended. (And yes, my dev store's shop.money_format is currently ${{ amount }} !!. It amused me at some point.)
Are you trying to use these liquid statements in the context of an app? If so, there may be a problem with that app's interpretation of the liquid code - but as you can see, from a purely Shopify standpoint, your code should be working exactly as expected (both with and without converting the string to a number) I would recommend contacting your app supplier to complain that things aren't working the way they should.
If you have only positive numbers, you can use the abs filter to convert the String to a number. This should the work like this:
{% assign teststring = '202.2400' %}
teststring as money: {{ teststring | abs | money: "GBP" }}
Of course, this only works correctly if the number is already positive.
According to this you should be able to do:
{% assign teststring = '202.2400' %}
{% assign testnum = teststring | times: 100 %}
teststring as money {{ testnum | money }}

How to group menu by Alphabet in shopify?

How to group menu by Alphabet in shopify?
I want to group dropdown menu like this.
I am new to shopify, I tried to group menu by trying to get the first letter using this code
{% assign first_letter = {{ link_primary.title | slice: 0 }} %}
but it is giving me following error
Liquid syntax error: Unexpected character { in "{{{{ link_primary.title | slice: 0 }} }}"
You don't actually need the {{ }} around the link title.
You can assign the first letter like:
{% assign first_letter = link_primary.title | slice: 0 %}
Then just check whether it's within a certain range, for example A-D, E-H etc.