on shopify, i want a line of code of code from theme.liquid to show only on collection pages regardless the template used (i have a few custom collection templates). how do i write the code to do this?
currently i'm using.
{% if template == 'collection.home' %}
<div class="filter_btn">
loremipsum
</div>
{% endif %}
{% if template == 'collection.featured' %}
<div class="filter_btn">
loremipsum
</div>
{% endif %}
similar duplicate of code above for other collection template
i will be adding new collections with new custom templates in the future, but that means i have to duplicate more of the same to cover new collection templates..
how can i have a line of code that covers all collection templates / collection pages
Use the contains operator instead:
{% if template contains 'collection' %}
I am likely a collection template
{% endif %}
See more about contains here:
http://docs.shopify.com/themes/liquid-documentation/basics/operators#contains
Related
I have create many collections like All Products, New Releases, T-Shirts, Pants etc. And I have two categories of products like Men's and Women's. Some collections specifically for Men's and some collections specifically for Women's. I create two tags Men's and Women's. I create menu by collections query by tags. My my products collection url is like this:
/collections/all-products/mens
/collections/all-products/womens
/collections/new-releases/mens
/collections/new-releases/womens
/collections/bras/womens
I want to show some text and menu list when collection.url show /mens or /womens.
{% if collection.url contains mens %}
do something
{% endif %
Above condition not working. I know why not working because {{ collection.url }} provide /collections/all-products. {{ page.url }} will not work for collection object. I haven't find any suggestion or liquid code reference where show men's or women's products in collections page it will show specific text.
If use in loop it will work.
{% for product in collection.products %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
<h3>Mens Products</h3>
{% endif %}
{% endfor %}
{% endfor %}
Above code will not work for me because it's inside loop. I need to show outside of loop. I don't understand how to achieve it. here is the reference site. Below have image how I want.
Need help!
You have access to the current_tags, refer to docs: https://shopify.dev/docs/themes/liquid/reference/objects/current-tags
This will return an array of all the tags you are viewing at the moment (in case you are viewing more than one tag).
So your check will be:
{% if current_tags contains 'mens' %}
do something
{% endif %}
That's pretty much the just of it.
I really like how the menu is coming along! Here are some ideas you could consider for your category specific menu, if you've not gotten where you want yet.
For your url strategy, what you're looking for is handle. Handles are specific to liquid. https://shopify.dev/docs/themes/liquid/reference/basics/handle
You could make a custom collection template if those 2 categories need to be fairly different: https://shopify.dev/tutorials/customize-theme-create-alternate-templates. If you do that, then you can use template_prefix from the collection object.
Assign a variable outside your loop and then set it inside the loop like:
{% assign is_mens = false %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
{% assign is_mens = true %}
{% endif %}
{% endfor %}
then {% if is_mens %} or {% unless is_mens %} for your dynamic content, or a case statement to define content specific to categories in your menu.
Hope this helps!
Just wondering why this is not populating products in the Shopify page template but it's working normally in the article template:
{%- assign firstsecthandle = page.metafields.first-sect.rev-handle -%}
{% for product in collections[ firstsecthandle ].products %}
...
{% endfor %}
As a result of this, I get the list of collections instead of products on page template but on the article template I get products.
On the page template it works only if I put the exact URL handle of collection:
But I need collection-handle to be dynamically declared not to have static value.
A variant which is working with a static 'collection-handle':
{% for product in collections.collection-handle.products %}
...
{% endfor %}
Using craft CMS I am adding an option to be able to hide a section from a PDF.
The PDF is created using PHP and a library called Snappy.
The light-switch is turned off (0) by default.
I have added the conditional statement and the light-switch, tested by wrapping other sections of the site (not part of the PDF) inside the if condition.
When turning the light-switch on and off I am able to hide and show sections.
The PDF does not seem to be working that way. The section is always hidden even when the light-switch is turned on.
I don't understand how the backend of the PDF works so is it something to do with how PDFs are generated?
Also it's showing blank pages where the content "would" be which is not desired.
//====Master PDF layout
{% extends 'abc/pdf/_base.html' %}
{% block content %}
{% include 'abc/pdf/_section1.html' %}
{% include 'abc/pdf/_section2.html' %}
{% include 'abc/pdf/_section3.html' %}
{% include 'abc/pdf/_section4.html' %}
{% include 'abc/pdf/_section5.html' %}
{% endblock %}
//==== Section with condition (abc/pdf/_section4.html)
{% if (entry.showsection4 is defined and entry.showsection4) and dateRanges | length %}
<div class="page">
<div class="page__inner">
A bunch of content in section 4 that I want to hide and show
</div>
</div>
{% endif %}
I am trying to check if my current page is a collection page not a single product page in some collection.
I mean for example if someone goes to collection page of shoes then I can check using collection.handle == 'Shoes' but if I select a product from that page then it will still give me true But I want my condition to be true on if it is collection page.
Thanks for your Help!
Use this simple way with template:
{% if template contains 'collection' %}
Do something
{% endif %}
As Shopify evolves you can now use this:
{% if template.name == 'collection' %}
Do something
{% endif %}
A safer alternative to avoid rare cases where templates are badly named would be to use request.page_type - see the Shopify docs
{% if request.page_type == 'collection' %}
Do something
{% endif %}
I would use:
{% if template == 'collection' %}
Do something
{% endif %}
Because if you use contains and not == (equal to) you may run risk of it changing down the line if you ever created a new template which contained the word collection?
You can try this in condition.
{%if request.page_type == "collection" %}
--- True if visit collection page or sub collection page. ---
{% endif %}
Thanks.
I'm trying to retrieve all Shopify collections for our store which have products matching tag dog.
{% for collection in collections %}
{% assign gato = 'false' %}
{% assign perro = 'false' %}
{% for tag in collection.tags %}
{% if tag == 'Cat' %}
{% assign cat = 'true' %}
{% elsif tag == 'Dog' %}
{% assign dog = 'true' %}
{% endif %}
{% endfor %}
{% if dog == 'true' and cat == 'false' %}
<li>{{ collection.title | link_to: collection.url }}</li>
{% endif %}
{% endfor %}
I successfully get this list when I'm at homepage (telepienso.com). (See footer screenshot: A). I have the same exact code in collection.liquid and I get some of the collections but NOT all of them. (telepienso.com/collections/all). (See list on the right screenshot: B). Is there any restriction inside collection.liquid which can affect?
A screenshot (productos para perros list):
B screenshot (sección perros list):
This was the cause of the problem. The collections variable was being paginated.
By moving the code in the question outside of the paginate liquid tag, all collections are displayed in the sidebar (the same as the footer).
EDIT: The above link is broken because the question was deleted due to low traffic. I've copied the content from the question below for reference.
Shopify - issue accessing the collections global variable inside paginate
I would like to be able to access the collections global variable from within a paginated group of products, but the collections variable is also paginated if it is accessed within a paginate liquid tag.
For example (in collection.liquid):
{% for collection in collections %}
{{ collection.title }}
{% endfor %}
<br />
{% paginate collection.products by 4 %}
{% for collection in collections %}
{{ collection.title }}
{% endfor %}
...
{% endpaginate %}
Output:
All Collection1 Collection2 Collection3 Collection4 Collection5 Collection6 Frontpage
All Collection1 Collection2 Collection3
The for loop before the paginate tag lists all collections as you would expect, but doing the same thing within the paginate tag causes collections to be paginated as well as the products I am actually wanting to paginate.
Is there a way to access the collections global variable within a paginated group of products without it also being affected by the pagination?
Why would I want to do this? It was causing this problem, and was not immediately obvious because the code using the collections variable was in a separate snippet to the code with the pagination.
EDIT 2: I can no longer reproduce this issue, it appears to have been fixed.