Liquid coding (Shopify) "and" and "elsif" not functioning together - shopify

I'm new to liquid coding while starting my own Shopify store (although I had some experience with coding). I'm currently stuck trying to get this code to function. I've added 3 text options in the liquid file for each variable. The idea is that when the criteria are met, it would display the appropriate text option. Please see the code string here for ref.
{% if section.settings.free_shipping_announcement_bar %}
{% assign promote_text = section.settings.promote_free_shipping_text | escape %}
{% assign unlocked_text = section.settings.unlocked_free_shipping_text | escape %}
{% assign unlocked_del_text = section.settings.unlocked_free_delivery_text | escape %}
{% assign threshold = section.settings.free_shipping_threshold | times: 100 %}
{% assign value_left = threshold | minus:cart.total_price %}
{% assign value_left_money = value_left | money %}
<div class="announcement-bar">
{% if value_left <= 0 %}
<p class="announcement-bar__message">{{unlocked_text}}</p>
{% elsif value_left >= 1 and value_left < 50 %}
<p class="announcement-bar__message">{{unlocked_del_text}}</p>
{% else %}
<p class="announcement-bar__message">{{promote_text | replace:'(value)' , value_left_money}}</p>
{% endif %}
basically, the idea is if the cart total is between $0-$99 I will show the "promote_text", if it's between $100-$149 it will show the "Unlocked_free_del_text"(free delivery), then at $150+ in the cart it shows "unlocked_text".
It currently shows the "promote_text" from $0-$149 then changes directly to the "unlocked_text" and skips the middle section for "unlocked_del_text"
my best assumption is that I'm using the elsif function wrong but I've trailed as many adjustments as I can think of without breaking the code, so any advice would be greatly appreciated!

Update:
After checking and testing the code over dev store I find you need to use divided_by to value_left value to compare with the value into normalized format.
so your code needs an update to this line
{% assign value_left = threshold | minus:cart.total_price | divided_by: 100%}
becase case is multile of 100 to process to cart value and need to normalized before test into if else condition.
The code looks great, I am not sure why this not works properly, you can check and try in this way also and I hope it will work. overwise you need to post the whole code along with schema and anyone replicate it dev store and find the solution.
{% if section.settings.free_shipping_announcement_bar %}
{% assign promote_text = section.settings.promote_free_shipping_text | escape %}
{% assign unlocked_text = section.settings.unlocked_free_shipping_text | escape %}
{% assign unlocked_del_text = section.settings.unlocked_free_delivery_text | escape %}
{% assign threshold = section.settings.free_shipping_threshold | times: 100 %}
{% assign value_left = threshold | minus:cart.total_price | divided_by: 100%}
{% assign value_left_money = value_left | money %}
<div class="announcement-bar">
{% if value_left <= 0 %}
<p class="announcement-bar__message">{{unlocked_text}}</p>
{% elsif value_left >= 1 and value_left < 50 %}
<p class="announcement-bar__message">{{unlocked_del_text}}</p>
{% else %}
<p class="announcement-bar__message">{{promote_text | replace:'(value)' , value_left_money}}</p>
{% endif %}
</div>
{% endif %}

Thanks for the quick response! I trialed the snippet you added but unfortunately ran into the same issue. here is the schema that I added to impact the section of code I input. If you are able to spot a simple issue in here please let me know, otherwise, I will continue to troubleshoot personally and hopefully find a solution or alternate method, to avoid troubling someone with having to replicate the entire code to recreate the issue.
{
"type":"checkbox",
"label":"enable free shipping bar",
"id":"free_shipping_announcement_bar",
"default":false
},
{
"type":"text",
"id":"promote_free_shipping_text",
"label":"message to promote free shipping"
},
{
"type":"text",
"id":"unlocked_free_shipping_text",
"label":"message for unlocked free shipping"
},
{
"type":"text",
"id":"unlocked_free_delivery_text",
"label":"message for unlocked free delivery"
},
{
"type":"range",
"id":"free_shipping_threshold",
"label":"threshold for free shipping",
"min":0,
"max":200,
"step":5,
"unit":"$",
"default":150
}

Related

Liquid If statement does not run else

I am trying to set class"price" two different types of data depending on if the IF statement is true/false, the If statement does the first part but if the else is true class"price" does not set the variable, anybody know why?
{% if dynamic_products.compare_at_price_max > dynamic_products.price %}
<p class="old-price"><span class="price-sep"></span><span class="price">Box
Value:{{ dynamic_products.compare_at_price | money }}</span></p>
{% else %}
<p class="old-price"><span class="price-sep"></span><span class="price">{{
'products.product.sold_out_html' | t }}</span></p>
{% endif %}

Shopify Liquid get every even block in forloop

i am trying to get every block that has an even position in shopify liquid inside a forloop :
{% for block in section.blocks %}
{% if forloop.index | modulo : 2 == 0%}
//some code
{%endif%}
{% endfor %}
but shopify returns me this error:
Expected end_of_string but found pipe in "forloop.index | modulo : 2
== 0"
Can somone help me solve this?
Thanks in advance :D
You need to split calculations from logic in liquid.
{% assign num = forloop.index | modulo: 2 %}
{% if num == 0 %}
// code
{% endif %}
So you must save the module calculation as variable and check it after that, you can't do the check and calculation at the same time.

Shopify order email template sometimes prints transaction twice

Sometimes (often, but randomly), the order confirmation prints out the payment gateway twice:
Payment
Credit card – 36.90
Credit card – 36.90
I've confirmed that real money is not being transferred twice. But could the transaction count be higher anyways? I'm not so sure about how the transaction_count works, and I don't really know how to debug this.
This part is the problem (I don't see where else it could be):
{% assign transaction_count = transactions | size %}
{% if transaction_count > 0 %}
<h4>Payment</h4>
{% for transaction in transactions %}
{% if transaction.status == "success" or transaction.status == "pending" %}
{% if transaction.kind == "authorization" or transaction.kind == "sale" %}
<p>
{{ transaction.gateway | replace: "_", " " | capitalize }}
—
<strong>{{ transaction.amount | money }}</strong>
</p>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
I've custom built the whole shop, but I can't seem to understand why this is happening.
I'm stumped, I've reached out to Shopify support but they don't want to help because I have added some custom html in the Order Notification email.
A huge thanks for anybody that could help me with this!
There is no other explanation than to assume that in
transactions there are more than one transactions. You can double check the transaction variable and see where the double identical transactions are coming from.

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.

Shopify: Order Print App is not picking up variables

I am trying to display a Final Sale message on order receipts for products that are 60% off. This bit of code does display the message on the individual product page, but when I insert in the template I'm using in the Order Printer app, the message does not seem to display.
I've contacted various Shopify support people, but they have not been able to identify the problem. Here's the code I'm inserting:
<!--if item is 60% off, it displays message: -->
{% if product.compare_at_price %}
{% assign sixtyPercentOff = product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price | round %}
{% if sixtyPercentOff == 60 %}
<p style="color: #B21F1F;">
This item is final sale -- no returns or exchanges are accepted.
</p>
{% endif %}
{% endif %}
Is it because Order Printer does not recognize variables such as "compare_at_price"?
product.compare_at_price doesn't exist.
For product you have: compare_at_price_max, compare_at_price_min, compare_at_price_varies
Do you mean to use something like: {% if product.compare_at_price_min > 0 %}
Or if you have variants you can use directly variant.compare_at_price
For first available variant of product: {% if product.variants[0].compare_at_price > 0 %}