Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
In my store I offer free samples. I want to swap the sample image, with the variant image of the product selected.
<tbody class="line-item-table__list"> {% assign num_samples = 0 %} {%- for line_item in cart.items -%} {% assign free_sample_product = all_products[settings.free_sample_product] %} {% assign free_sample_variant_id = free_sample_product.selected_or_first_available_variant.id %} {% if line_item.variant.id == free_sample_variant_id %} {% assign num_samples = num_samples | plus: line_item.quantity %} // Here is where I would like to swap the images {% assign line_item.image = //this will not work// free_sample_variant.image%} {% endif %}
I need to reference the variant image, but I am not sure how to reference it recursively. Any help would be appreciated.
Hi @md24
{% assign line_item.image = //this will not work// free_sample_variant.image%}
Make your own variable logic to replace any line_item.image
⚠️This does not account for putting the image into an <img> tag or other html.
{% liquid # free samples vars
assign num_samples = 0
assign free_sample_product = all_products[settings.free_sample_product]
#assumes first available variant is always the correct variant
assign free_sample_variant_id = free_sample_product.first_available_variant.id
assign custom_line_item_image = free_sample_variant.image
%}
{%- for line_item in cart.items -%}
{% liquid
if line_item.variant.id == free_sample_variant_id
assign num_samples = num_samples | plus: line_item.quantity
# variable to swap the images
# !!! use boolean logic to replace any line_item.image references in this loops context
assign use_custom_image = true
else
assign use_custom_image = false
endif
%}
...
{% if use_custom_image %}{{ custom_line_item_image }}{% else %}{{ line_item.image }}{% endif %}
...
{% endfor %}
Good Luck.
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org