Liquid: Assigning and capturing existing custom attribute in the control flow code block - conditional-statements

The purpose of this control flow block is checking to see if the existing level attribute is blank or an empty string, if it is, I would like to assign a default Level 1 to the code block. If the level attribute exists and is not blank, I would like to grab the current value of the customer's level. Somehow the Liquid engine is throwing an error in my code.
Can someone pinpoint where I've done wrong in my code? Thanks so much!
{% if customer.level == blank or customer.level =='' %}
{% capture customer.level %}Level 1{% endcapture %}
Hey! Your rewards level is: {{ customer.level }}
{% else %}
Customer level is: {{ customer.level }}
{% endif %}

In Shopify's Liquid, objects cannot be changed. Your attempt to assign a value to customer.level is failing because you are not allowed to overwrite, delete or add values in something like the customer object. (The . between customer and level indicates that "level" is a property of the "customer" object - customer.level does not represent a single variable on its own)
To fix the code, all you need to do is come up with a variable name that does not use the . character (or any other punctuation that can be interpreted by the code parser). For example, naming your variable customer_level (using an underscore) should do the trick for you.
Also note that level is not a property of a customer object in Shopfiy, and you cannot add arbitrary fields to the core object types. I would recommend storing your level as either a tag or a metafield on the customer if you aren't already. Note that assigning an appropriate tag or metafield cannot be done through the Liquid code, it has to be added by some app or process that has appropriate permissions.

Related

Liquid does not print object.field property

I'm new to Liquid and I'm trying to create a Mechanic script in Shopify to loop through all my orders and check each SKU. This is what I came up with:
{% for line_item in order.line_items %}
{% if line_item.sku %}
{{ line_item.sku }}
{% capture sku %}
On the third line, I was expecting to see it print an order's SKU, which should look like this: 1xSAMPLE1+2xSAMPLE2. It, however, showed me this error:
lexical error: invalid char in json text.
1xS
(right here) ------^
Typically, this comes down to a stray comma, or some other character that makes for invalid JSON.
Context (lines 2-5 of rendered JSON):
As I understand it, Mechanic (or Liquid) only wants to print JSON values. How do I view my variables while running the Mechanic script?
Here is the full code and the error log:

Targeting a Collection - Liquid

Greetings Shopify Pals!
I have a website at: https://www.blakesseedbased.com/ (password: TopSecretPass)
The Goal:
We will have products that vary in quantity and would like to display this quantity information between the product title and price from the featured home page products (raspberry, chocolate chip, pineapple).
The Issue:
Currently we're able to add content to this area, but we cannot seem to get the if else statement to work correctly. We have a collection called "Snack Bars" however our logic isn't recognising this and injecting the proper code. We tried changing the collection Snack Bars to lower and uppercease snack-bars/Snack-Bars but no luck.
The Logic:
IF the product item is from the Snack Bar collection, add the box quantity (box of 12).
Else, add "no quantity"
The Code:
{% if product.collections contains 'snack-bars' %}
<span>Box of 12</span>
{% else %}
No Box Amount
{% endif %}
Thanks in advance!!
Greetings Birdie Golden!
The issue you're hitting is that product.collections is an array of Collection objects when you're accessing it in Liquid, and no collection object will ever equal a string value.
Luckily, we can easily create an array of handles using the map filter and assign that to a variable, like this:
{% assign collection_list = product.collections | map: 'handle' %}
{% if collection_list contains 'snack-bars' %}
...
The map filter above will create a new array using just the value that you pass it, so the collection_list variable will be an array of just-the-handles. Since handle is a string, we can use contains as expected to see if the value we want is in the list.
Hope that helps!

Shopify Liquid Tag within Asset Tag

I'm trying to insert a metafield variable within an asset url as below:
{% assign review = product.metafields.review %}
{% assign key = 'rating' %}
<img src="{{ '[review.rating].png' | asset_url }}"/>
For some reason it isn't returning the actual variable, instead the text itself, is there a way to go about doing this?
If any reviews actually exist at the namespace product.metafields.review then you have to iterate through them. When you do that, for each iteration you'll get some key value pairs. With those you can print out the actual data of the metafield resources. What you are attempting there in your snippet seems a bit off. Try accessing the rating key in your iterator, and if it exists, the value would be available to you for your image snippet.

Shopify - Capturing a Metafield Value of the Last Added Collection with a Custom Template

I'm working on a theme where I've got several collections of clothing and I have created a custom collection template called 'seasonal', with the intent of using a custom background color for that specific type of collection.
I've achieved this by using metafields. It's working fine and I now have two seasonal collection pages - FW15 and SS16, with two different background colors,
I would now like to fetch the background color of the most recent 'seasonal' collection in another page of the theme.
This is where I'm stuck in. See the code below:
{% for collection in collections reversed %}
{% if collection.template_suffix contains 'seasonal' %}
{% assign seasonalCollectionColor = collection.metafields.c_f.Collection_Color | split: "|" %}
{{ seasonalCollectionColor[0] }}
{% endif %}
{% endear %}
This is outputting both seasonal collection colors:
#8DE5EB #FF7C1A
Instead of just the most recent one, which I'm trying to get by appending [0] to seasonalCollectionColor.
Any help on what am I doing wrong?
Thanks in advance!
You are in a loop. The loop has one condition you are triggering off of (contains seasonal). Hence you capture the result twice, and output it twice. You are not limiting your test to the condition of most recent.
If you were to try and test for the condition of most recent, what would that be? In your case, perhaps since you are traversing collections in a reverse order, the first occurrence is your most recent one.
So add a condition that if the condition is found, and you have set the seasonalCollectionColor to something, DO NOT set it again... just ignore any other values that match, and then you'll have only one value, the most recent one.

Shopify - Template check within product-loop

Does anybody know that is possible to check the template within product-loop? So, if one of the listed product's template is different, somehow I need to know.
Any idea?
You can use the global template variable to access the name of the template used to render the current page. For example:
{% if template contains 'product' %}
This is a product page...
{% endif %}
Or you might want product.template_suffix:
Returns the name of the custom product template assigned to the product, without the product. prefix nor the .liquid suffix. Returns nil if a custom template is not assigned to the product.
Input
<!-- on product.wholesale.liquid -->
{{ product.template_suffix }}
Output
wholesale