How scan an Order with Shopify Liquid - for customised Invoice - shopify

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 %}

Related

How to display current parent active category and its sub-categories only in Sidebar?

Hey I would like to display only active parent category and its available sub-categories in the category-tree in storefront/layout/sidebar/category-navigation.html.twig.
so if category A is selected then it should display all its subcategories.
example
Category A
Category A.1
Category A.2
Category A.3
This will output the tree branch to the currently active category, as well as the links to its direct descendant categories.
{% sw_extends '#Storefront/storefront/layout/sidebar/category-navigation.html.twig' %}
{% block layout_navigation_categories_list_entry %}
{% if (item.category.id in activeResult.id) or (item.category.id in activeResult.path) or (item.category.parentId in activeResult.id) %}
{{ parent() }}
{% endif %}
{% endblock %}
Update: If you want to hide the parents up to the currently viewed category, this should do the trick.
{% block layout_navigation_categories_list_entry %}
{% if item.category.id in activeResult.path %}
{% block layout_navigation_categories_recoursion %}
{% if item.category.id in activeResult.id %}
{% set levelIncrement = 1 %}
{% else %}
{% set levelIncrement = 0 %}
{% endif %}
{% sw_include '#Storefront/storefront/layout/sidebar/category-navigation.html.twig' with {
navigationTree: item.children,
activeResult: activeResult,
level: level + levelIncrement
} only %}
{% endblock %}
{% endif %}
{% if (item.category.id in activeResult.id) or (item.category.parentId in activeResult.id) %}
{{ parent() }}
{% endif %}
{% endblock %}

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

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 %}

Add a class to div if a product is in the cart

Is it possible to see if an item is already in the cart and add a class to div on the product page?
I was hoping something like:
{% for line_item in cart.items %}
{% if line_item.title = product.title %}
{% assign incart = "in-cart" %}
{% endif %}
{% endfor %}
would do this: {% if line_item.title = product.title %} but Shopify doesn't like it. Ideas?
line_item.title is different than product.title. Because line_item.title is the variant of the product which is in the cart.
Say product title is Shirt
And the line_item.title will be Shirt-Red(red variant)
So try this line_item.product.title
Try something like this:
{% assign in_cart = false %}
{% for item in cart.items %}
{% if item.product.handle == product.handle %}
{% assign in_cart = true %}
{% endif %}
{% endfor %}
{% if in_cart == true %}
Product is already in the cart...
{% endif %}
Also note that in your if statement you should use ==, not =.

Shopify: calling swatch.liquid 'colors' on collection.liquid and product-loop.liquid

I have not been able to find a fix for my issue. I want to show a simple "More Colors" option beneath the product prices.
Screenshot of Product example
I have been testing code such as:
{% if product.variants 'color' > 1 %}
Has more than one variant.
{% else %}Only one variant.
{% endif %}
But haven't had any luck. Everything either calls "Has more..." or "Only one..." regarless of the swatch count.
I am editing product-loop.liquid which is being called from collections.liquid
It has only outputed "Only one variant" or "Has more than one variant" for every product regardless of swatch/color count.
Thanks for any help..
This statement is nonsense:
{% if product.variants 'color' > 1 %}
Instead, you want to check the options set for your variants, and if you detect an option set to color, and you have more than one variant, then clearly, you are in more than one color territory.
{% assign has_more_colors = false %}
{% for option in product.options %}
{% if option contains 'color' and product.variants.length > 1 %}
{% assign has_more_colors = true %}
{% endif %}
{% endfor %}
So now you can do whatever it is you need to do... since you now know you a) have an option called colors, and b) more than one variant, hence more than one color...
The following snippet was posted to the Shopify boards. It solved this issue:
{% assign option_title = "Color" %}
{% assign option_index = "" %}
{% assign option_values = "" %}
{% for option in product.options %}
{% if option == option_title %}
{% assign option_index = forloop.index %}
{% endif %}
{% endfor %}
{% for variant in product.variants %}
{% assign variant_option_value = "" %}
{% if option_index == 1 %}
{% assign variant_option_value = variant.option1 | handleize %}
{% elsif option_index == 2 %}
{% assign variant_option_value = variant.option2 | handleize %}
{% elsif option_index == 3 %}
{% assign variant_option_value = variant.option3 | handleize %}
{% endif %}
{% assign option_values = option_values | append:"," | append:variant_option_value %}
{% endfor %}
{% assign option_values = option_values | remove_first:"," | split:"," | uniq %}
{% if option_values.size > 1 %}
MORE COLORS
{% else %}
{% endif %}