Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I've created Custom liquid,
{% if product.selected_variant %} {{ product.selected_variant.metafields.custom.new_desc}} {% else %} {{ product.description }} {% endif %}
but if I change the variant, the content doesn't change.
Anyone know how to fix this?
Solved! Go to the solution
This is an accepted solution.
Liquid code runs only on the Shopify servers. When people change selectors on your product page theme Javascript updates your page to reflect those changes.
Theme JS has no idea that it needs to update your text. You'd need to have your own JS to do it.
This would be different for different themes.
Say, for Dawn I'd be doing something like, but this should be a "Custom liquid" block under Product Information section, not a separate section.
<div class="custom-description">
{{ product.selected_or_first_available_variant.option1 }}
</div>
<script>
window.addEventListener('DOMContentLoaded', () => {
subscribe (PUB_SUB_EVENTS.variantChange, (event) => {
const { html, variant, sectionId } = event.data;
debugger;
const source = html.querySelector(`[data-section=${sectionId}] .custom-description`);
const destination = document.querySelector(`[data-section=${sectionId}] .custom-description`);
if (source && destination) destination.innerHTML = source.innerHTML;
});
});
</script>
Also -- you may consider using an app for this https://apps.shopify.com/dynamic-variant-description
This is an accepted solution.
Liquid code runs only on the Shopify servers. When people change selectors on your product page theme Javascript updates your page to reflect those changes.
Theme JS has no idea that it needs to update your text. You'd need to have your own JS to do it.
This would be different for different themes.
Say, for Dawn I'd be doing something like, but this should be a "Custom liquid" block under Product Information section, not a separate section.
<div class="custom-description">
{{ product.selected_or_first_available_variant.option1 }}
</div>
<script>
window.addEventListener('DOMContentLoaded', () => {
subscribe (PUB_SUB_EVENTS.variantChange, (event) => {
const { html, variant, sectionId } = event.data;
debugger;
const source = html.querySelector(`[data-section=${sectionId}] .custom-description`);
const destination = document.querySelector(`[data-section=${sectionId}] .custom-description`);
if (source && destination) destination.innerHTML = source.innerHTML;
});
});
</script>
Also -- you may consider using an app for this https://apps.shopify.com/dynamic-variant-description
It works!
Thank you
Hi @matusmachus,
Please send me the store link, I will check it for you
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024