I use the Impact Theme.
I would like my custom Liquid code in Shopify to change on the product page when the variant is changed.
With this code:
{% for variant in product.variants %}
{% if product.selected_or_first_available_variant.id == variant.id %}
<div class="variant-description">
{{ variant.metafields.custom.beschreibung }}
</div>
{% endif %}
{% endfor %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
// Funktion, um den Text basierend auf der ausgewählten Variante zu aktualisieren
function updateVariantDescription() {
var selectedVariantId = {{ product.selected_or_first_available_variant.id | json }};
var description = '';
{% for variant in product.variants %}
if ({{ variant.id | json }} == selectedVariantId) {
description = {{ variant.metafields.custom.beschreibung }};
}
{% endfor %}
$(".variant-description").text(description);
}
// Bei Änderung der Variante aktualisieren
$(document).on('change', ".single-option-selector", function(){
updateVariantDescription();
});
// Initialen Text festlegen
updateVariantDescription();
});
</script>
This is the code that I’ve written in the Custom Liquid block.
The text only changes after reloading the page. Can someone help me?