Shopify liquid: How can I conditionally include snippets in Shopify liquid? - api

I would like to include a snippet in a template but only if the snippet file exist. Is there any way I can do it?
Now I'm just using:
{% include 'snippetName' %}
But this throws the error:
Liquid error: Could not find asset snippets/snippetName.liquid
The reason I need such a functionality is because I have a background process that adds the snippet later on.

Had this problem myself. This was my solution:
{% capture the_snippet_content %}{% include the_snippet %}{% endcapture %}
{% unless the_snippet_content contains "Liquid error" %}
{% include reviews_snippet %}
{% endunless %}
Basically capture the snippet’s content as a variable.
If there is no snippet Shopify generates the error:
Liquid error: Could not find asset
snippets/caroline-flint-reviews.liquid
So check to see if it’s generated that… if so don’t print the snippet
:D
Of course this would break if you intended your snippet to include "Liquid error" or if Shopify ever change the error message.

Extending on Jon's answer;
Create a file called snippet.liquid
{% capture snippet_content %}{% include snippet %}{% endcapture %}
{% unless snippet_content contains "Liquid error" %}
{{ snippet_content }}
{% endunless %}
Then when you want to include a file only if it exists
{% include 'snippet' with 'filename_of_include' %}

Okay, Coming here in 2021.
The include syntax is deprecated and infrequently used, also extending #a.wmly answer, this should be the latest syntax replacing include with render:
{% capture snippet_content %}{% render 'your-snippet-name' %}{% endcapture %}
{% if snippet_content contains "Could not find asset" %}
{% comment %} do nothing {% endcomment %}
{% else %}
{% render 'your-snippet-name' %}
{% endif %}
references for include vs render : https://shopify.dev/docs/themes/liquid/reference/tags/deprecated-tags#include

Alternatively, you could create your own tag which does a check on the existence of the file, before attempting to process it.
https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#create-your-own-tags

#vovafeldman Not sure why you can't have a blank snippet, but there's no file exists.
The only other option I can think of is since you are using a BG process to generate the snippet (and I assume upload it), you can always use the template API to upload the version of the template that includes the snippet at the same time.

Using the code listed above by Jon or a.wmly both still gave me errors. However, simply writing
{% include 'snippet_name' %}
worked just fine.
Note that this only worked for files located in the "snippets/" folder. So Templates, for instance, did not work using this method.

Related

Accentuate Custom Fields | Get blog posts by reference handle

I have a new site I'm setting up whereby blog posts contain related products and the products contain related blog posts. I can make this happen easily enough by adding product references on blog articles and blog references on products.
My main issue with this approach is that the client will need to do double entry. What I'd like to do is simply add a product reference to the blog posts and then on the product page, loop all blog posts and display any that have a reference to the current product handle.
I have a variation of this working to some extent:
{% assign related_posts = "" %}
{% for article in blogs.news.articles %}
{% if blog.metafields.blog_info.linked_products contains product.handle %}
{% capture post %}
<li><p>{{ article.title }}</p></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
However I have 2 problems:
1.) Shopify limit the return of blogs to 50 articles, so it isn't searching all posts
2.) If it did work on all posts, would this significantly slow down the page?
Does anybody know how to access all blog articles in one go, or have a better idea for how to implement this functionality?

How to combine for and where clause in Liquid

I'm currently working on a shopify site and need to create a filter on a products page. My goal is to have primer paints show up in a different section of the product collection page. What I'm having trouble with is how to filter for primers. We have a tag set up in the product page named "Primer". What I want is for the loop to check whether a product has the primer tag, and if so, display that as one of the products in the loop. I'm relatively new to Liquid, so I don't know how to combine clauses if that's possible. I've looked up the "Where" clause, but don't entirely understand how it works.
Here is the code as it stands:
<div class="{% if settings.show_collection_sidebar %}desktop-10{% else
%}desktop-12{% endif %} tablet-6 mobile-3" id="bside">
<div id="product-loop">
{% for product in collection.products %}
<div class="product {% if settings.products_per_row == '3' %}
desktop-4{% cycle ' first', '', ' last' %}
{% elsif settings.products_per_row == '4' %}
desktop-3{% cycle ' first', '', '', ' last' %}
{% endif %} tablet-half mobile-half"
id="prod-{{ product.id }}"
data-alpha="{{ product.title }}"
data-price="{{ product.price }}">
{% include 'product-listing' %}
</div>
{% endfor %}
</div>
</div>
How could I filter for the desired results? I've tried {% if product.tags contains 'Primer' %} in quite a few places, but to no avail.
Thanks for your help.
Have you considered using two collections to accomplish what you're after?
If you made a dynamic ("Smart") collection for all your primers (Let's assume the handle for the collection is 'primer'), you can access that collection at any time through Liquid:
{% assign primer_collection = collections['primer'] %}
{% for product in primer_collection.products %}
<h2>HAVE SOME {{ product.title }}!</h2>
{% endfor %}
Then, if you wanted to exclude all of your primer products from the main collection, create a collection named 'all' (or at least with the handle 'all' - the actual title doesn't strictly matter - see footnote). By default, the 'All' collection is, true to its name, every product in your store. However, if you create your own 'all' collection, you can define it to mean "Everything except certain items" - in your case, 'everything but the primer'
Having Shopify pre-filter everything for you through the collections themselves greatly reduces the headaches of trying to apply filters after-the-fact and dealing with misleading item counts, uneven pagination, etc.
Handles: At the bottom of each collection and product in your Shopify admin is the SEO settings. Editing these allows you to change what the handle for the collection/product is, and this is what Shopify uses to look up your collection/product internally

Shopify: Why does Liquid sometimes use {%- instead of {%?

I've been looking through the code of the default theme, and I've noticed that sometimes they use the {% tag to indicate Liquid code (as per the documentation), but other times they use {%-.
For example:
{% case section.settings.image_size %}
{% when 'small' %}
{%- assign product_image_width = 'medium-up--one-third' -%}
{%- assign product_description_width = 'medium-up--two-thirds' -%}
I cannot find a single example of {%- in the Liquid documentation, either on the Shopify site, or on GitHub, but I've seen other people use it on the Shopify forums, too.
What does the addition of a - signify?
The hyphens are a new syntax option that you can use to suppress blank lines that would otherwise show up in the source. You can learn more about this here: https://shopify.github.io/liquid/basics/whitespace/

Shopify Metafields If Statement

For some reason, when I wrap this metafield within an if statement it seems to break, but works perfectly when out of the statement. The product I'm looking at has a rating set within metafields of '3' but still shows 'Not Been Rated Yet' which is really odd!
{% assign review = product.metafields.review %}
{% assign key = 'rating' %}
{% if product.metafields.rating != blank %}
<img src="//cdn.shopify.com/s/files/1/1513/9752/t/3/assets/{{ review.rating }}.svg"/>
<span>Scored {{ review.rating }}/5 with a Verified Tester</span>
{% else %}
<img src="//cdn.shopify.com/s/files/1/1513/9752/t/3/assets/unrated.svg"/>
<span>Not been rated yet. Become a tester!</span>
{% endif %}
Would anyone be able to help with this?
This was fixed due to a name space error. As you're already within a 'product' you don't need to retell Shopify that you want to access the product... field.
Fixed by trying the if statement against 'review.rating != blank'

Hide specific product in Shopify with Liquid based on customer tags

I'm looking to hide a specific product in Shopify from all customers except those with a certain tag on their account.
I was going to use the app Lockdown but it's been discontinued.
So far everything I've learned says that I use something like this:
{% if customer.tags contains 'Bundle' %}
{% include 'product-to-be-shown' %}
{% endif %}
That second line is where I'm stuck, I think I'm on the right track but I'm not entirely sure.
Any guidance is appreciated, even if that guidance takes me an entirely route to get the solution I'm looking for.
Close.. but no cigar...
On your product template... use this instead...
{% if customer.tags contains 'Bundle' %}
Show this product as usual.
{% else %}
Politely tell this customer there is nothing here for them
{% endif %}
That is all any lockdown or other wholesale App does anyway... absolutely nothing special...