Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi everyone,
I'm using this code and it works fine, it displays the % discount but I want to display the % discount depending on the variant selected, any idea please?
<span class="discount_product_page">
{% assign percentage = 0 %}
{% for variant in product.variants %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% endfor %}
{{ percentage }}%
</span>
Example page here https://www.golfcenter.fr/products/callaway-golf-supersoft-blanche?variant=40227097542692
Solved! Go to the solution
This is an accepted solution.
I've got it :
{% assign percentage = 0 %}
{% for variant in product.variants %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% assign discount = product.compare_at_price | times:percentage | divided_by: 100 %}
{% assign price = product.compare_at_price %}
{% endfor %}
<span class="ProductItem__Price Price Price--highlight Text--subdued" style="text-transform:lowercase">à partir de {{ price | minus: discount | money }}</span>
Hi @GolfCenter ,
This is Victor from PageFly - Landing page builder, I’d like to suggest this ideaTo display the % discount depending on the variant selected, you can modify the code to include an if statement that checks if the current variant is selected, and if it is, then display its discount percentage. Here's an example code snippet that demonstrates this:
{% for variant in product.variants %}
{% if variant == selected_variant %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{{ saving }}% off
{% endif %}
{% endfor %}
In this code, selected_variant
refers to the currently selected variant. The code loops through all the variants of the product and checks if the current variant is equal to the selected variant. If it is, then it calculates the discount percentage and displays it.
Note that you would need to define the selected_variant
variable before this code, so that it contains the currently selected variant.
Hope my answer will help you.
Best regards,
Victor | PageFly
I apologize for the confusion. Here's an updated version of the code that should work:
{% assign percentage = 0 %}
{% for variant in product.variants %}
{% if variant == selected_variant %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% endif %}
{% endfor %}
{{ percentage }}%
In this code, we start by initializing the percentage
variable to 0. We then loop through all the variants of the product and check if the current variant is equal to the selected variant. If it is, we calculate the discount percentage using the formula you provided, and update the percentage
variable to the higher of its current value or the calculated percentage (using the at_least
filter). Finally, we display the percentage
variable.
Note that you would need to define the selected_variant
variable before this code, so that it contains the currently selected variant. You can do this using the following code:
{% assign selected_variant = product.selected_or_first_available_variant %}
Thank you for providing the link to the product. I took a look at the page, and I can see the issue you're facing. The problem is that the code is only executed once when the page loads, so it displays the discount percentage for the initially selected variant. When you select a different variant, the code doesn't re-run, so the displayed discount percentage doesn't change.
To solve this issue, we can use JavaScript to update the discount percentage when the variant is changed. Here's an example code snippet that demonstrates this:
{%- if product.type == 'Balles' -%}
{% assign selected_variant = product.selected_or_first_available_variant %}
{% assign percentage = 0 %} {% for variant in product.variants %}
{% if variant == selected_variant %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% endif %}
{% endfor %}
<span id="discount_product_page">{{ percentage }}%</span>
<script>
// Update discount percentage when variant is changed
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll('[name="id"]').forEach(function(radio) {
radio.addEventListener("change", function() {
var percentage = 0;
var selected_variant = JSON.parse(this.dataset.variant);
{% for variant in product.variants %}
{% if variant == selected_variant %}
var saving = {{ variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round }};
percentage = Math.max(percentage, saving);
{% endif %}
{% endfor %}
document.getElementById("discount_product_page").textContent = percentage + "%";
});
});
});
</script>
{% endif -%}
In this code, we first display the discount percentage using a span
element with the id
attribute discount_product_page
. We then use JavaScript to add an event listener that listens for changes to the selected variant. When a variant is changed, the event listener calculates the discount percentage for the newly selected variant and updates the text content of the span
element to display the new percentage.
I hope this solution works for you!
Thanx for you help and your job but still not working.
I see, thank you for letting me know. It's possible that the app is interfering with the code.
Hi victor could you please help me for this please ? The result I'm expecting is in bold
{% assign percentage = 0 %}
{% for variant in product.variants %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% assign discount = product.compare_at_price | times:percentage | divided_by: 100 | money %}
{% endfor %}
<span class="ProductItem__Price Price Price--highlight Text--subdued">I WOULD LIKE THIS PART TO BE : (PRODUCT.COMPARE_AT_PRICE) MINUS (DISCOUNT)</span>
<span class="ProductItem__Price Price Price--compareAt Text--subdued">{{ product.compare_at_price | money }}</span>
This is an accepted solution.
I've got it :
{% assign percentage = 0 %}
{% for variant in product.variants %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% assign discount = product.compare_at_price | times:percentage | divided_by: 100 %}
{% assign price = product.compare_at_price %}
{% endfor %}
<span class="ProductItem__Price Price Price--highlight Text--subdued" style="text-transform:lowercase">à partir de {{ price | minus: discount | money }}</span>
Hello there!
Can you please tell me where exactly you placed this code?
With kind regards
Marcel
Hi, my variant code is not working. Please suggest the solution.
it working
{% assign selected_variant = product.selected_or_first_available_variant %}
{% assign percentage = 0 %}
{% for variant in product.variants %}
{% if variant == selected_variant %}
{% assign saving = variant.compare_at_price | minus: variant.price | times: 100 | divided_by: variant.compare_at_price | round %}
{% assign percentage = saving | at_least: percentage %}
{% endif %}
{% endfor %}
<span id="discount_product_page" class="sela-off">{{ percentage }}% OFF</span>
<script>
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll('[name="id"]').forEach(function(radio) {
radio.addEventListener("change", function() {
var selectedVariantId = this.value; // Get the selected variant ID
var variants = {{ product.variants | json }}; // Get the variants data from Liquid
var selectedVariant = variants.find(function(variant) {
return variant.id == selectedVariantId;
});
if (selectedVariant && selectedVariant.compare_at_price > selectedVariant.price) {
var saving = Math.round(((selectedVariant.compare_at_price - selectedVariant.price) / selectedVariant.compare_at_price) * 100);
document.getElementById("discount_product_page").textContent = saving + "% OFF";
} else {
document.getElementById("discount_product_page").textContent = "0%";
}
});
});
// Trigger the change event to update the discount on initial load
var initialRadio = document.querySelector('[name="id"]:checked');
if (initialRadio) {
initialRadio.dispatchEvent(new Event('change'));
}
});
</script>
By 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, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024