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

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

Related

shopoify liquid tags if else unless

I need to customize the shipping confirmation email. I want to use a tag to determine which of two text sections are included in the email. The problem is there is usually an array of tags. I can get section "A" like this...
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% endfor %}
There is only a single 'a' tag in the array so I only get the "A" text once.
But I can't figure out how to get the "B" text to appear just one time.
If I do this, it appears for every tag that does not == 'a'...
{% for tag in tags %}
{% unless tag contains 'a' %}
B
{% endunless %}
{% endfor %}
Is there a way to get one instance of "B"?
You could repeat the same logic you did for A:
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% if tag == 'b' %}
B
{% endif %}
{% endfor %}
Alternatively you could do a switch/case statement, I'd prefer this approach because it's easy to read, and if sometime in the future you would like to add another condition (tag), it would be easy and the code would still keep its elegance.
{% for tag in tags %}
{% case tag %}
{% when 'a' %}
A
{% when 'b' %}
B
{% when 'c' %}
C
{% endcase %}
{% endfor %}
If you have many tags it can become tricky, but if it's just two tags this is the general idea.
{% assign a_not_found = true %}
{% for tag in tags %}
{% if tag == 'a' %}
...
{% assign a_not_found = false %}
{% endif %}
{% endfor %}
{% if a_not_found %}
{... show b... }
{% endif %}
Otherwise
{% if tags contains 'a' %}
{...show a...%}
{% else %}
{...show b...%}
{% endif %}

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 Liquid - Related products as specific handles from a metafield

I am trying to use a Shopify metafield with a comma separated list of handles (ie: handle1,handle2) to call specific related products. These related products are displayed on individual product pages. My problem is: I cannot figure out how to get the products from the array to iterate and display.
I am using the Boundless theme, so I am trying to call/display the products in the same manner as a collection page. This may be part of my problem.
My current code calls the actual product on the page instead of the related products for some reason.
Here is my current code:
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for product in shownwith %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{% endif %}
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for relPro in shownwith %}
{% assign product = all_products[srelPro] %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{{ shownwith_items}}
{% endif %}

How to hide products in Shopify search results based on a vendor name

I'm in a situation where hiding a certain vendors products in the control panel isn't an options due to an outside POS. For a test in search.liquid, I used search.terms like below. This code works but not everyone will type thevendor exactly the same way and will see the products if they don't type thevendor.
{% for item in search.results %}
{% if search.terms == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
I tried to figure out how to write the code to hide these products in a better way. I tried product.vendor like below but when I search for those products individually they are not hidden. The code:
{% for item in search.results %}
{% if product.vendor == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
Can someone tell me what I'm missing here? It seems it doesn't know what product.vendor is but when I print out who the vendor is, it displays the vendor. I don't understand why it's not hiding the products that are associated with this vendor.
{% for item in search.results %}
{% if item.product.vendor == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
This should work.

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 =.