Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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 [email protected] 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
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024