Shopify Metafields If Statement - shopify

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'

Related

For loop limit problem when query by tags (Shopify Liquid)

I'm new in Shopify theme development. I'm stuck in for loop query. for loop not working properly when try to query by tag. Below is my code.
{% assign query_tag = block.settings.home_section_tag %}
{% if collections.new-releases.products.size > 0 %}
{% for product in collections.new-releases.products limit:4 %}
{% if product.tags contains query_tag %}
<div class="col-12 col-md-6 col-lg-3">
<h3>{{ product.title | escape }}</h3>
</div>
{% endif %}
{% endfor %}
{% endif %}
Above code show me only 1 product it supposed to be 4 products. Also limit:1, limit:2, limit:3 not working (No product showing), limit:4 show me 1 product, limit:5 show me 2 products, limit:6 show me 3 products, limit:7 show me 3 product, limit:8 show me 4 products (it's on going). if I remove product query by tag limit: working properly. I don't understand what is the issue with my code.
I need help for this issue and need an explanation why it's not working.
It works correctly. Initially, you get first 4 products from the new-releases collection. And then you filter the result by the query_tag within the loop.
You can:
Increase the limit to 50 (not good if your collection has more items), or
Create another collection filtered by the query_tag and use for loop on that collection limiting to 4

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>

Printing Product Variant HS Codes

I am trying to create an automated Commercial Invoice within Shopify, using the Liquid template language. I have everything working, except for the IMPORT/EXPORT Harmonized Codes (HS Tariff Codes) that are stored as variant meta-fields. Whenever I try to print them out using the following code, I get blanks:
{% for line_item in line_items %}
{{ line_item.variant.metafields.global_harmonized_system_code }}
{% endfor %}
Can someone help me pull these HS Codes for each product variant and print them on the Commercial Invoice using liquid to pull the meta-field?
Global is a namespace, try :
{{ line_item.variant.metafields.global.harmonized_system_code }}
The syntax is :
{{ your_object.metafields.namespace.key }}
Your Liquid is insufficient for the task at hand.
{{ line_item.variant.metafields.global_harmonized_system_code }}
That output is not valid. It might point to a set of one or more key value pairs, so you should really iterate on that. Example:
{% for mf in line_item.variant.metafields.global %}
{% if mf | first == 'harmonized_system_code' %}
<p> {{ mf | last }} how is that for some value! </p>
{% endif %}
{% endfor %}
Something like that is more precise and will go through the variant metafields allowning you to choose which ones to print out.
I am able to get the value using this
{{ line_item.variant.metafields.harmonized_system_code.value }}

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

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