shopify pass a variable to settings - shopify

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks

Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}

Related

Go through array elements in Liquid

I'm writing some liquid code for a shopify website in order to add products to any blog post based on tags that use product's handle. I've tried with one product and it works just fine so I was trying to iterate it through a loop but I couldn't get any information from the array I've created. This is the code I've written so far. Can you help me understand what I'm doing wrong? Thanks!
{% comment %}declare variables{% endcomment %}
{% assign related_prod_index = 0 %}
{% assign related_prod_array = "" | split: ',' %}
{% comment %}check for tags that contains products handles{% endcomment %}
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
{% assign related_prod_array = related_prod_array | push:blog_prod %}
{% assign related_prod_index = related_prod_index | plus:1 %}
{% endif %}
{% endfor%}
{% comment %}check how many product tags I found{% endcomment %}
<h1>{{ related_prod_index }} tags found</h1>
{% comment %}loop that create small preview for each product{% endcomment %}
{% if related_prod_array %}
{% for rel_pr in related_prod_array %}
<img src="{{ rel_pr.featured_image | img_url:'original' }}">
<p>{{rel_pr.title}}</p>
<p>{{rel_pr.price | money}}</p>
{% endfor %}
{% endif %}
This looks like it should be working, but if this post is true, then you can't add Product objects to an array.
The good news is you shouldn't need to. Put the display code in the same loop you use to search through the tags.
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
<img src="{{ blog_prod .featured_image | img_url:'original' }}">
<p>{{blog_prod .title}}</p>
<p>{{blog_prod .price | money}}</p>
{% endif %}
{% endfor %}

Checking if no compare_price exists in Shopify Liquid

I want to check if there's no compare price, and I cannot get any of the following to work in Shopify:
{% if price > compare_at_price %}
{% if compare_at_price == 0 %}
{% if compare_at_price == "" %}
I want to output some HTML when the compare_price doesn't exist.
You are missing the object here to get its attributes:
{% if product.price > product.compare_at_price %}
Do something
{% endif %}
To check if there is one:
{% if product.compare_at_price %}
Do sthg
{% endif %}
To check if there isn't one:
{% unless product.compare_at_price %}
Do sthg
{% endunless %}
Documentation:
https://shopify.dev/docs/themes/liquid/reference/objects/product

Shopify Liquid - Related products as specific handles from a metafield

I am trying to use a Shopify metafield with a comma separated list of handles (ie: handle1,handle2) to call specific related products. These related products are displayed on individual product pages. My problem is: I cannot figure out how to get the products from the array to iterate and display.
I am using the Boundless theme, so I am trying to call/display the products in the same manner as a collection page. This may be part of my problem.
My current code calls the actual product on the page instead of the related products for some reason.
Here is my current code:
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for product in shownwith %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{% endif %}
{% if product.metafields.c_f['Shown With'] %}
{% assign shownwith = product.metafields.c_f['Shown With'] | split: ',' %}
{% capture shownwith_items %}
{% for relPro in shownwith %}
{% assign product = all_products[srelPro] %}
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %}
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %}
{% endfor %}
{% endcapture %}
{{ shownwith_items}}
{% endif %}

can i use in_array () in Shopify

I am new To Shopify ! i am trying to something like,i want to check that,the ID which i put on Textbox is in array or not !
here is my Product.Liquid
{% if settings.make_an_offer %}
{% for id in product.id %}
{% include 'pricewaiter' %}
{% endfor %}
{% endif %}
in above code,i need to check that settings.make_an_offer is in this array product.id or not..
make_an_offer this is an ID of my Textbox
so how can i do this?
any help please?
Place it like this :
{% for id in product.id %}
{% if settings.make_an_offer === id %}
{% include 'pricewaiter' %}
{% endif %}
{% endfor %}

Add a class to div if a product is in the cart

Is it possible to see if an item is already in the cart and add a class to div on the product page?
I was hoping something like:
{% for line_item in cart.items %}
{% if line_item.title = product.title %}
{% assign incart = "in-cart" %}
{% endif %}
{% endfor %}
would do this: {% if line_item.title = product.title %} but Shopify doesn't like it. Ideas?
line_item.title is different than product.title. Because line_item.title is the variant of the product which is in the cart.
Say product title is Shirt
And the line_item.title will be Shirt-Red(red variant)
So try this line_item.product.title
Try something like this:
{% assign in_cart = false %}
{% for item in cart.items %}
{% if item.product.handle == product.handle %}
{% assign in_cart = true %}
{% endif %}
{% endfor %}
{% if in_cart == true %}
Product is already in the cart...
{% endif %}
Also note that in your if statement you should use ==, not =.