How to auto update message on side drawer/Cart in Shopify? - shopify

I have my message in my side-drawer and cart page, it work's but I have to refresh to see the message. I read and is said that I need to use cart.js. But I no knowledge. If someone can help me. Below is my code.
{% if cart.total_price >= 500000 %}
<div class="cartMSG">You got a free Gift</div>
{% endif %}

Related

Shopify Additional Scripts

Within Shopify's "Additional Scripts" section of the Checkout, I need to add a script (below) for tracking purposes, and for the life of me I cannot get the cart item quantity to render.
Apparently I should be able to use Liquid syntax to render the value, but whenever I do a test, the value is empty.
Below is my script that isn't working.
<script language='JavaScript1.1' async src='//pixel.trackingcompany.com/event/js?mt_id=123&mt_adid=456&mt_exem=&mt_excl=&s1={{ cart.item_count }}'></script>
When it renders, I currently get everything except the {{ cart.item_count }} value.
There is no cart item in the checkout process.
You should swap cart.item_count with checkout.line_items.size or order.line_items.size.
Thanks Drip! I was able to resolve this using the code below. It the CART variable is not available once the checkout has occurred, so changing to the checkout as you suggested and looping through the line items did the trick!
{% assign count = 0 %}
{% for line_item in checkout.line_items %}
{% assign count = count | plus: line_item.quantity %}
{% endfor %}
<script language='JavaScript1.1' async src='//pixel.trackingcompany.com/event/js?mt_id=123&mt_adid=456&mt_exem=&mt_excl=&s1={{ count }}'></script>

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

What if my attribute name in Shopify liquid has a space? (Customer.io email system)

I'm attempting to add some logic in Customer.io, which uses Shopify's Liquid markup language:
{% if customer.Campaigns Participated > 0 %} I also saw that you participated in a campaign, which is awesome!
If you have a second, I'd love it if you could tell me what it was that made you sign up?
{% else %}
I see you haven't had a chance to participate in your first campaign - can I ask what happened?
{% endif %}
The problem is that it doesn't recognize the "Campaigns Participated" property due to the space...
Is there any way I can escape this space or something?
Thank you
customer.['Campaigns Participated'] is your only hope.

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

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.