Shopify - Exit for loop through if statement - shopify

I am working a shopify shop but not trying to sell products but rather appointments.
I am using a shopify extension called Sesami to do so.
In the customer/account.liquid page I want to show the next coming appointment (or appointments) and a different information to people who don't have any future appointment coming.
I have been trying to do this with the following code :
{% if customer.orders != blank %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign today_date = 'now' | date: '%s' %}
{% assign pre_date = property.last | date: '%s' %}
{% if pre_date >= today_date %}
{{ line_item.product.title | link_to: line_item.product.url }}
{% for property in line_item.properties limit:1 %}
{% if property != blank %}
{{ property.last | date: format: 'abbreviated_date' }}
{{ 'customer.orders.at' | t }}
{% endif %}
{%- endfor -%}
{% for property in line_item.properties offset:1 %}
{{ property.last }}
{%- endfor -%}
{{ line_item.image | img_url: 'small' | img_tag }}
{{ order.fulfillment_status_label }}
{% endif %}
{% endfor %}
{%- endfor -%}
{% endfor %}
{% else %}
Content for people with no booking at all
{% endif %}
But the problem is that the forloop stays open and therefore shows the content I am hoping to display to people with no upcoming appointment multiple times based on the total number of past appointments.
I imagine there is a much simpler way to do this and am hoping you can help me find it !
Thanks a lot,
Julien

Consider using {% break %} when you'd like the loop to stop it's current iteration.
https://shopify.github.io/liquid/tags/iteration/#break

Related

Debug liquid for Shopify

I'm really puzzled and can't understand why my {{ time }} returns an empty value.
I'm trying to calculate the difference between the time currently and a booking time in Shopify. For the booking I have the date (first line_item.property) and the time (second one) coming separately. I want to add one to another and then compare it to the current time, but before I can do the math I have tried to display the different variables I have created.
{{ now }} & {{ date }} work fine but for {{ time }} it returns an empty value. On the other hand if instead of assigning it a value name I just display the line.item.property it works just fine.
Can you help me understand what I am doing wrong here?
{% assign today = 'now' | date: '%s' %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign date = property.last | date: '%s' %}
{% endfor %}
{% for property in line_item.properties offset:1 %}
{% assign time = property.last | date: '%s' %}
{% endfor %}
{%- endfor -%}
{{ now }} - {{ date }} - {{ time }}
{% endfor %}
By using plus: 0 you can convert the string to integer, this will enable math operation for your comparison.
{% assign today = 'now' | date: '%s' | plus: 0 %}
check with {% unless line_item.properties == empty %} to see if properties exist.
{% assign date = property.last | date: '%s' %} , the variable property.last must follow a well-formatted dates for date to work. liquid-date-format
{% for property in line_item.properties offset:1 %}
{% assign time = property.last | date: '%s' %}
{% endfor %}
problem with offset:1, if the array only has 1 line_item.properties this will not run at all. Hence time is empty; or it exist but property.last does not have a date format.
{% assign today = 'now' | date: '%s' | plus: 0 %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% unless line_item.properties == empty %}
{% for property in line_item.properties %}
{% if forloop.index == 1 %}
{% assign date = property.last | date: '%s' | plus: 0 %}
{% if date == 0 %}
{% comment %}Opp! Not A Date, Terminate loop{% endcomment %}
{% break %}
{% endif %}
{% else %}
{% assign time = property.last | date: '%s' | plus: 0 %}
{% unless time == 0 %}
{% assign date = date - time %}
{% endunless %}
{% endif %}
{% endfor %}
{% endunless %}
{% endfor %}
{{today - date}}
Thanks Charles that really unblocked me.
Posting here the final version I went with as I needed to compare exact time and not simply the date of the order.
{% assign now_date = 'now' | date: '%s' | plus: 0 %}
{% assign now_time = 'now' | date: '%s' | plus: 0 %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign booking_date = property.last | date: '%s' | plus: 0 %}
{% endfor %}
{% for property in line_item.properties offset:1 limit:1 %}
{% assign booking_time = property.last | date: '%s' | plus: 0 %}
{% endfor %}
{% assign date_to_compare = booking_date | plus: '86400' %}
{% unless now_date > date_to_compare %}
{% if now_time <= booking_time or now_date <= booking_date %}SEANCE A VENIR{% endif %}
{% endunless %}
{%- endfor -%}
{% endfor %}

Checking if no compare_price exists in Shopify Liquid

I want to check if there's no compare price, and I cannot get any of the following to work in Shopify:
{% if price > compare_at_price %}
{% if compare_at_price == 0 %}
{% if compare_at_price == "" %}
I want to output some HTML when the compare_price doesn't exist.
You are missing the object here to get its attributes:
{% if product.price > product.compare_at_price %}
Do something
{% endif %}
To check if there is one:
{% if product.compare_at_price %}
Do sthg
{% endif %}
To check if there isn't one:
{% unless product.compare_at_price %}
Do sthg
{% endunless %}
Documentation:
https://shopify.dev/docs/themes/liquid/reference/objects/product

How scan an Order with Shopify Liquid - for customised Invoice

We are producing Invoices with Shopify's 'Order Printer' app.
and want to customise the Invoice.
For instance, if they have bought a 'book' - we want it to say "Enjoy your book"
and if a 'CD' - "Enjoy the music".
I've found I can test the first item they purchased with 'limit:1' :
{% for line_item in unfulfilled_line_items limit:1 %}
productType: {{ line_item.product.type }} - prodtype:{{product.type}} <br/>
{% if line_item.product.type contains "cd" %}
its a CD <br/>
{% else %}
it's not a CD?)<br/>
{% endif %}
{% endfor %}
But I would really like to scan the whole of the product.type array to determine how many of each product type there are - and output either/both messages - with plural 's' as appropriate.
Any ideas?
You're on the right track instead of limiting though you basically want to count.
{% assign cd_count = 0 %}
{% assign book_count = 0 %}
{% for line_item in unfulfilled_line_items %}
{% if line_item.product.type == "cd" %}
{% assign cd_count = cd_count | plus: 1%}
{% endif %}
{% if line_item.product.type == "book" %}
{% assign book_count = book_count | plus: 1 %}
{% endif %}
{% endfor %}
cd count: {{ cd_count }}
book count: {{ book_count}}
Now that you have a count you should be able to just do an if statement of the count numbers.
Thanks #trowse - solved the zeros issues, they were due to OrderPrinter cache problems and limitations. Just in case anyone needs it. Here's our solution:
<!-- count how many of each product type we are/have????? fullfilling -->
{% assign count_cd = 0 %}
{% assign count_bk = 0 %}
{% for line_item in unfulfilled_line_items %}
{% if line_item.product.type contains "cd" %}
{% assign count_cd = count_cd | plus:1 %}
{% endif %}
{% if line_item.product.type contains "Book" %}
{% assign count_bk = count_bk | plus:1 %}
{% endif %}
{% endfor %}
<!--- end of counting -->
<!-- Enjoy.. message -->
{% if {{count_cd > 0 %}
Enjoy the music
{% if {{count_bk > 0 %}
and the {{ count_bk | pluralize: 'book', 'books' }}<br/>
{% endif %}
{% else %}
{% if {{count_bk > 0 %}
Enjoy the {{ count_bk | pluralize: 'book', 'books' }}<br/>
{% endif %}
{% endif %}

Shopify - How to export Line item prices after discount?

Exporting discounted line items seems to be a common problem. I need to export the line item prices to my accounting software.
I'm trying to check for a coupon AND a specific product TAG, then apply the math with liquid to the actual discounted price.
I feel like I'm close but I'm striking out.
Any Advice would be much appreciated.
{% if line_items.tags contains '001' AND line_items.discounts == true %}
{{ line_item.price | times: .75 }}
{% else %}
{{ line_item.price }}
{% endif %}
Something like this?
{% for line_item in line_items %}
{% if line_item.product.tags contains '001' and line_item.discounts %}
{% assign rebate = 0 %}
{% for discount in line_item.discounts %}
{% if discount.type == 'PercentageDiscount' %}
{% assign rebate = line_item.price | times:discount.amount %}
{% endif %}
{% endfor %}
{{ line_item.price | minus:rebate }}
{% else %}
{{ line_item.price }}
{% endif %}
{% endfor %}

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}