Shopify Display Alt Image for each Variant - shopify

I have variant images in a shopify account, each image has its own alt tag. I want to have the individual alt tags display under each image. If I use the {{ image.alt | escape }}, it just displays one alt name for all the variant images. So if image 01 has "alt 01", and image 02 has "alt 02", and I try to display them on the page, both display "alt 01". Any things on utilizing the images alt fields?
{% for image in product.images %}
<li data-thumb="{{ image | product_img_url: 'large' }}" data-title="{{ image.alt | escape }}">
<a href="{{ image | product_img_url: '1024x1024' }}" class="fancybox" rel="group" title="{{ image.alt | escape }}">
<img src="{{ image | product_img_url: 'large' }}" alt="{{ image.alt | escape }}" itemprop="image" data-cloudzoom="zoomImage: '{{ image | product_img_url: '1024x1024' }}', tintColor: '{{ settings.shop_bg_color }}'" class="cloudzoom" /><br />
{{ image.alt | escape }}
</a>
</li>
{% endfor %}

Make sure you have provided alt tags for each of the images in the Products page of the Admin.
See here:
I tested your code, and when alt tags are not set it outputs the product title under each image. After entering alt text for each image (from the Products page in the Admin area), your snippet worked as expected and outputted the corresponding alt tag under each product image.

Related

Shopify Liquid - Get the URL for a variant's image

On a Shopify product page, I can loop through all the variants and get information about them like this:
{% for variants in product.variants %}
{{ variants.id }}<br>
{{ variants.sku }}<br>
{{ variants.title}}<br>
{{ variants.price | divided_by: 100}}<br>
{% endfor %}
This works perfectly fine, however when I try and get the variant image in the loop like this:
{{ variants.image}}
I have also tried using:
{{ variants.featured_image.src}}
Which produces exactly the same result.
The output is simpy products and then the name of the image file. For example 'products/my-image.jpg'.
This is obviously not the path to the image and when I try and display this within an image tag, a broken image is produced.
I am trying to get the path to the image that is stored on Shopify's CDN for this variant product, which will look something like https://cdn.shopify.com/s/files/1/0022/4572/2212/products/WI1M1L1F1B1_grande.jpg?v=1656698226.
Does anyone know how this can be achieved within the loop I am using above?
You are looking for this
{{ variant.image.src | product_img_url: '200x' }}
I suggest you to take a look here to find many other useful functions: https://www.shopify.ca/partners/shopify-cheat-sheet
Also is useful to download a free theme (Dawn for example) and use it as reference.
Here is a code example to show each variant image and url :
<div class="product_thumbnails">
{% for variant in product.variants %}
<a href="{{ variant.url }}">
<div class="h-[50px] w-[50px] bg-[#E6ECEB] rounded shrink-0 p-1">
{{ variant.image | image_url: width: 50 | image_tag }}
</div>
</a>
{% endfor %}
</div>

Capture two variables in liquid

What I want to do:
Capture two variables {{section.settings.{{title}}_modal_title}}
Context:
The links in the linklist are not really used as links, but purely so user can reorganize it in their Shopify portal.
External javascript file creates elements (modals) on click event, value from section.settings.XXX_modal_title is supposed to be used in the created modals.
The linklist:
<ul class="menu">
{% for link in linklists.main-menu.links %}
<li data-menu="{{ link.title | upcase }}" class="menu-link">
<p class="menu-title">[{{ link.title | upcase }}]</p> <-- Click event
</li>
{% endfor %}
</ul>
What I tried:
My first try was adding the liquid tag itself with .insertAdjacentHTML('afterbegin', {{section.settings.${attLowerCase}_modal_title}}) when the element is created, but soon realized that liquid renders the values first. The output of doing this is literally "{{ }}"
My second try was to capture two variables and put it in a hidden input.
{% assign title = link.title | downcase %}
{% capture section_title_variable %}
{{section.settings.{{title}}_modal_settings}}
{% endcapture %}
<hr class="item-line">
<p class="ueq-menu-title">[{{ link.title | upcase }}]</p>
<input type="hidden" id="about_modal_title" value="{{section_title_variable}}">
In depth example:
section.settings
{
"type": "text",
"id": "about_modal_title",
"label": "About window title",
"default": "ABOUT"
},
User clicks on link ABOUT
<ul class="menu">
{% for link in linklists.ueq-menu.links %}
<li data-menu="{{ link.title | upcase }}" class="menu-link"> <-- "ABOUT"
{% assign title = link.title | downcase %} <-- downcase to "about"
{% capture section_title_variable %}
{{section.settings.{{title}}_modal_title}}
{% endcapture %}
<hr class="item-line">
<p class="menu-title">[{{ link.title | upcase }}]</p>
<input type="hidden" id="about_modal_title" value="{{section_title_variable}}"> <-- captured should look like this {{section.settings.about_modal_title}}
</li>
{% endfor %}
</ul>
on click event, get the value from hidden input, and use it when creating elements.
"Why not use {% javascript %}?"
I prefer to do it this way, since most of my script files are external. I like to keep it that way.
"Why capture it like that?"
The links in the linklist are already known, this was in the design. Thats why in sechema one of them is called "about_modal_title". Since this is used inside a for loop, this was the only way I could come up with to connect the title and schema settings with each other.
If someone knows a better way to do this instead of putting it in a hidden input, please let me know :)
In proper Liquid syntax, curly-brackets are never embedded inside curly-brackets
The syntax {{ something_{{ you }}_want }} is illegal Liquid syntax - once an expression is started, using either {{ or {%, everything up to the next }} or %} is evaluated as one template command.
In your example, you're looking to combine two strings in order to get the name of a setting that you want to access. In a dedicated programming language, we would expect there to be nice shortcuts for something like this. Liquid, however, is first and foremost a templating language, and doesn't have such shortcuts. Instead, we will need to accomplish our goal in two steps.
The first thing we need to know is that we can access properties in two ways: {{ settings.fieldname }} is equivalent to {{ settings['fieldname'] }}
Thanks to that second syntax, we can access an arbitrary setting using a variable:
{% assign field = 'fieldname' %}
{{ settings[fieldname] }}
So to do something more complicated, we just need to use assign (or capture - just be aware that capture includes whitespace as well!) to get a variable that contains the name of the field we want to access, then pass that in to the square brackets:
{% assign title_field = link.title | downcase | append: '_modal_title' %}
{{ section.settings[title_field] }}

Shopify border selected color

I have shopify shop and i found a code on internet to replace the color by his image like this.
I really need to add a border when the color is selected, can someone help me please.
the code is to do this is :
{%if option.name == "Couleur"%}
<label>{{option.name}}</label>
{%assign index = forloop.index %}
{%for value in option.values%}
<input class="single-option-selector-{{ section.id }}" id="Couleur-{{forloop.index}}" type="radio" style='visibility:hidden;display:none' name="Couleur" value="{{ value | escape }}" data-index="option{{index}}" {% if option.selected_value == value %}checked{%endif%}/>
<label id="colorSelected" class="label_couleur" for="Couleur-{{forloop.index}}" style="margin: 0 5px;">
<img class="img_couleur" src="{{value | escape | append:'.jpg' | strip | asset_url }}"/>
</label>
{%endfor%}
{%else%}
I really need help thanks in advance.
You can take reference in the Shopify community forum, provided a solution to create swatch images for color in products. Please click here to check to achieve.
This solution will not work venture and boundless theme. I implement in debut theme is working for client store.

Dynamic Attribute Names in Shopify Cart

I'm so close on this, but I just can't get the syntax right. I've been messing with this off and on for days. Essentially I have a greeting card message and I want the message to be filled in by the customer for every Greeting Card on the checkout page (cart.liquid) and I need the attribute name to change for every index. Therefore I'm adding the index to each attribute name, but to no avail. For testing purposes, here's a basic input field:
<p class="cart-attribute__field" style="min-width:300px;">
<label for="to{{ forloop.index }}">To:</label>
<input class="checkMe" id="to{{ forloop.index }}" type="text" name="attributes[To{{ forloop.index }}]" maxlength="40" data-stk="{{item.id}}" value="{{ cart.attributes['To'+forloop.index] }}" >
</p>
And its this part (value="{{ cart.attributes['To'+forloop.index] }}") that is giving me trouble.
You cannot use '+' operator in liquid code to append. Try this instead:
{% assign cart_attr = 'To' | append: forloop.index %}
<p class="cart-attribute__field" style="min-width:300px;">
<label for="to{{ forloop.index }}">To:</label>
<input class="checkMe" id="to{{ forloop.index }}" type="text" name="attributes[To{{ forloop.index }}]" maxlength="40" data-stk="{{item.id}}" value="{{ cart.attributes[cart_attr] }}" >
</p>
Note: Improvise as required.
Why would you not just use line item properties as described on https://docs.shopify.com/manual/configuration/store-customization/page-specific/product-page/get-customization-information-for-products

Shopify: issue with url image in linklists

i'm pretty new to Shopify and I'm facing a strange issue in linklists.
Yesterday I changed the featured image for a category I displayed into a linklist, but I cannot see the changes in the page that prints the linklist.
I analyze the .liquid file that prints the linklists and I found the snippet that produces the divs:
{% for link in linklists[linklist].links cols: 4 %}
<div class="products item {{ link.handle }}">
<a href="{{ link.url }}" title="Browse our {{ link.object.title | escape }} collection.">
<img src="{{ link.object.image.src | collection_img_url: 'large' }}" alt="{{ link.object.title | escape }}" />
<big>{{ link.title }}</big>
</a>
</div>
{% endfor %}
After some shots I tried to add a data attribute to the image to print again the link.object.title:
{% for link in linklists[linklist].links cols: 4 %}
<div class="products item {{ link.handle }}">
<a href="{{ link.url }}" title="Browse our {{ link.object.title | escape }} collection.">
<img src="{{ link.object.image.src | collection_img_url: 'large' }}" alt="{{ link.object.title | escape }}" data-test="{{ link.object.image.src | collection_img_url: 'large' }}" />
<big>{{ link.title }}</big>
</a>
</div>
{% endfor %}
The strange fact is that it prints two different values for the same object!
<img src="https://cdn.shopify.com/s/files/1/0407/7545/files/trousers-woman_c4633f02-59f7-4a4b-809b-91662635ddc0.jpg?22734" alt="Women's Trousers" data-test="//cdn.shopify.com/s/files/1/0407/7545/collections/DSC_9685_grande_df826b7f-5645-4491-b866-8819c9ad8983_large.jpg?v=1429273629">
The src attribute shows the old image, and the test attribute shows the new one.
Is that because Shopify postprocess the src attributes of the image for caching them into their cdn? How can I fix this?
thanks to #Jason input I found a javascript script that changed the attribute "src" of the image:
$('.collection-woman .webshop .trousers a img').attr('src','https://cdn.shopify.com/s/files/1/0407/7545/collections/DSC_9685_grande_df826b7f-5645-4491-b866-8819c9ad8983_large.jpg?v=1429273629');