How can I display percentage discount based on selected variant?

Topic summary

A Shopify developer seeks to dynamically display percentage discounts that update when customers select different product variants. The original code only calculates discounts on page load and doesn’t respond to variant changes.

Initial Problem:

  • Static discount display using Liquid templating
  • Percentage doesn’t update when users select different variants

Proposed Solutions:

  • PageFly-Victor suggests combining Liquid code with JavaScript event listeners to detect variant changes and recalculate discounts dynamically
  • Code checks if variant == selected_variant and uses formula: (compare_at_price - price) * 100 / compare_at_price

Complications:

  • GolfCenter reports the solution doesn’t work, possibly due to conflicts with the Bcpo app (Variant Options Product Options from RelentlessApps)
  • Third-party variant selector apps may interfere with standard Shopify variant change detection

Resolution:

  • GolfCenter eventually solves their specific display formatting issue (posts #10)
  • User ‘alphabeta’ shares a working solution using DOMContentLoaded event listener that monitors [name="id"] radio buttons and triggers discount recalculation on change
  • The working code includes initial page load trigger and updates a <span id="discount_product_page"> element

Status: Multiple working solutions provided, though implementation depends on specific theme/app configuration.

Summarized with AI on November 6. AI used: claude-sonnet-4-5-20250929.

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 %}
{{ percentage }}%

{% 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!