Twig check if value is in array - arrays

I'm having trouble to check if a value is present in an array with Twig.
I want to hide a shipping method in a checkout if there's a certain product in the cart.
I can only use Twig code so I have to find a logic in that.
So let's say when product ID 1234 is in cart then I want to hide #certain_div
So what I have is this ->
{% if checkout %}
{% set array = theme.sku_shipping_rule | split(',') %}
// theme.sku_shipping_rule = a text string like 1234, 4321, 5478
{% if checkout.products %}
{% for product in checkout.products %}
{% if product.sku in array %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
The problem I'm facing is that it seems my code always returns true. So even if the product.sku doens't match a value in the array it still hides #certain_div. I've tested that with placing {{ product.sku }} just before <style>.
What do I wrong?
Any help greatly appreciated!
UPDATE:
I've updated the question/code to show what's happening
{% if checkout %}
{% set skuToCheck = theme.sku_shipping_rule | split(',') %}
{% set skuInCart = [] %}
{% if checkout.quote.products %}
{% for product in checkout.quote.products %}
{% set skuInCart = skuInCart | merge([product.sku]) %}
{% endfor %}
{% endif %}
{% for myVar in skuInCart %}
{{ myVar }}<br/>
{% endfor %}
// this prints
PSYGA1 // where this sku should NOT match
FP32MA4
{% for myVar in skuToCheck %}
{{ myVar }}<br/>
// this prints
FP32LY4
FP32STR4
FP32MA4
{% if myVar in skuInCart %} // also tried with | keys filter
{{ myVar }} is found
{% endif %}
{% endfor %}
{% endif %}
So what I did is placing the sku's from the products which are in the cart in an array skuInCart. Next I want to check if myVar is present in the skuInCart array. If so print myVar is found.
What happens is that you should expect that it prints only the matching results. However it actually prints all values present skuInCart (using keys filter) or completely blank without using keys filter.

What you are doing in theory should work, have a look a this fiddle example to show you a working demonstration:
https://twigfiddle.com/yvpbac
Basically:
<div id="certain_div">
This should not show up
</div>
{% set searchForSku = "890" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
{% set searchForSku = "891" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
Will result in:
<div id="certain_div">
This should not show up
</div>
<style>
#certain_div {
display: none;
}
</style>
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>

You can use iterable to check if a variable is an array or a traversable object:
{% if items is iterable %}
{# stuff #}
{% endif %}

Related

Shopify - Add Number (1,2,3) before Product in Collection

I want the collection page to look like a numbered list (1. product 2. product 3. product ...) with a number in front of each product title.
In the product-grid-item.liquid I found the title and added {{ forloop.index }} in front of it but nothing shows up. When I add {% for product in collection.products %}{% endfor %} around it, it shows "123456789 title". When I add {% for product in collection.products %} before the top div and {% endfor %} after the bottom div, it repeats the whole collection multiple times.
When I use {% cycle '1', '2', '3', '4', '5' %} it shows "1." in front of every product.
What am I doing wrong? Are there other ways? Any help would be appreciated.
Okay, so this bellow code is for default Dawn theme from Shopify.
You need to edit the main-collection-product-grid.liquid and then navigate to code render 'card-product' and pass the foorloop.index as counter
on file card-product.liquid use it before the title
Need to add some logic to calculate correct number, add this code to calulcate it
{%- if paginate.pages > 1 -%}
{% assign page_size = paginate.page_size %}
{% if paginate.current_page > 1 %}
{% assign currPage = paginate.current_page | minus: 1 %}
{% assign loop_item = page_size | times: currPage %}
{% assign loop_item = loop_item | plus: forloop.index %}
{% else %}
{% assign loop_item = forloop.index %}
{% endif %}
{% else %}
{% assign loop_item = forloop.index %}
{% endif %}

CRAFT CMS 3 loop matrix field - variable entry does not exist

I'm trying to loop a matrix field that has one block containing 3 items.
{% for block in entry.galeria.type('itemsGaleria') %}
{% if block.titulo|length %}
{{ block.titulo.first }}
{% endif %}
{% endfor %}
But craft always throws the error variable entry does not exist.
I read the matrix section from craft 3 docs but cannot fix this problem.
Any clues?
Well, as there where no sugestions, and i never give up, i figured out for myself :)
Here it is:
{% set entries = craft.entries.section("galeria").all() %}
{% for entry in entries %}
{% for block in entry.galeriaMatrix.all() %}
{% switch block.type %}
{% case "itemsGaleria" %}
{% for image in block.fotografia %}
<img src="{{image.url}}" alt="{{image.title}}" />
{% endfor %}
{{ block.titulo }}
{{ block.texto }}
{% default %}
{% endswitch %}
{% endfor %}
{% endfor %}
It does what i need, which is loop all the entrances in the matrix block field.

Shopify liquid product.thumbnail.liquid repeats block even there is no loops

[![enter image description here][1]][1]My job Is too simple i.e just to add Amazon url in a div block in product thumbnail.liquid.I Have added just simple div, then I found that same div is repeated twice. I have also checked that there is no forloop found still how does it is repeating .Today I found the file called product-loop.liquid which has for loop and product thumbnail.liquid is included. What I need to do if I need to show amazon link block only once? Entire file is in gist link.Thanks.
product-loop.liquid
{% assign product_found = false %}
{% assign skip = false %}
{% assign collection_group = products | map: 'id' %}
{% assign collection_group_thumb = collection_group | append : 'thumb' %}
{% assign collection_group_mobile = collection_group | append : 'mobile' %}
{% capture new_row %}
<br class="clear product_clear" />
{% endcapture %}
<div itemtype="http://schema.org/ItemList" class="products">
{% for product in products limit: limit %}
{% if product.id == skip_product.id or skip == true %}
{% assign product_found = true %}
{% else %}
{% if forloop.rindex0 == 0 and product_found == false and forloop.length != products.count and template != 'search' %}
{% assign skip = true %}
{% else %}
{% include 'product-thumbnail', sidebar: sidebar %}
{% if products_per_row == 2 %}
{% cycle collection_group: '', new_row %}
{% elsif products_per_row == 3 %}
{% cycle collection_group: '', '', new_row %}
{% elsif products_per_row == 4 %}
{% cycle collection_group: '', '', '', new_row %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</div>
may be you including product.thumbnail in a section and that section have loop or conditional logics.

Liquid (Shopify) scope questions

I'm trying to set two variables outside of a few nested loops (for loops) and then check or reassign the variables inside the for. Basically, I'm trying to check for current_tags and display links to any tags under those tags (in the hierarchy I've got). Shopify seems to only support one level of hierarchy (collections and their associated products), but I'm trying to use these tags to make it look like I've got multiple levels of ways of classifying products. So shirt, then red shirt/brown shirt, then red silk shirt/brown silk shirt, as a shitty example.
Here's some code:
{% assign showtag = false %}
{% for link in linklists[settings.main_linklist].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
link {{link.title}}<br>
temp {{temp_tag}}<br>
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% for link in linklists[link.handle].links %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
{% if linklists[link.handle] == empty %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% else %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
The above is a little messy but is properly indented on the website. Anyway, it seems like the variable showtag loses scope and cannot be found in the for loops - any solutions?

Wrong parent loop context in twig

I'm using Symfony 2.3 and Twig 1.15. I've got a nested foreach twig loop and I'm trying to get a different result for the last iteration of the outer loop.
I've seen this: http://twig.sensiolabs.org/doc/recipes.html#accessing-the-parent-context-in-nested-loops
However I'm getting a different result - an error in the line where I access the parent context:
Key "loop" for array with keys "groups, scores, type, user, assetic, app, avatarsDir, sonata_block, _parent, _seq, group, _key, subgroup" does not exist in "(...)"
the relevant code, stripped of classes, ids and unnecessary tags:
{% for group in groups %}
<div>
{% for subgroup in group.subgroups %}
{% for test in subgroup.tests %}
{% block test_block_box %}
{% if not loop.parent.loop.last %}
(html follows...)
{% else %}
(some different html follows...)
{% endif %}
{% endblock %}
{% endfor %}
{% endfor %}
</div>
{% endfor %}
I have made sure that the error does not refer to the inner loop call, i.e. I replaced loop.parent.loop.last with loop.last and the page rendered successfully (contents obviously wrong, but it didn't crash).
What am I doing wrong when accessing the parent context??
Simply remove {% block test_block_box %} and {% endblock %}, the loop parent shoud be accessible.
You can try to define a value outside of the {% block %} and see if you have access to it in the {% block %}:
{% for group in groups %}
<div>
{% for subgroup in group.subgroups %}
{% for test in subgroup.tests %}
{% set test_loop_is_last = loop.last %} {# define the value #}
{% block test_block_box %}
{% if not test_loop_is_last %}{#test the value #}
(html follows...)
{% else %}
(some different html follows...)
{% endif %}
{% endblock %}
{% endfor %}
{% endfor %}
</div>
{% endfor %}

Resources