Radio Button Price Delta functionality (t shows the difference in price of variants)

Looking for a theme that calculates dynamic price deltas (selected variant price - other variant price) and displays that in the variant picker.


← This is just a mock up, but I want our product page to look exactly like this. Note the +$XXX in the actual button copy.

My inspiration is the way Apple does their configurations:

They make it easy to think whats the cost breakdown as you are configuring a high ticket item..

I searched for themes that support this, but cant find anything.

I was considering getting my hands dirty to modify the Dawn 9.0 theme we are currently using. If I went with this approach I guess there will be two parts:

1). Calculate the price deltas. in the global.js, make a method called updatePriceDeltas() which returns an objects with variant names as keys and the difference between (the price of that variant) - (the price of the selected variant).

updatePriceDeltas() {
  var productVariants = {{ product.variants | json }}; // Get the product variants from Liquid and convert to JS object

// Assuming the selectedVariantId is available
var selectedVariantPrice = productVariants.find(variant => variant.id === selectedVariantId).price;

var priceDifferences = {}; // Initialize empty object to store price differences

productVariants.forEach(variant => {
  priceDifferences[variant.id] = variant.price - selectedVariantPrice;
});

console.log(priceDifferences); // Outputs the object with variant IDs as keys and price differences as values

return(priceDifferences);
  }

2). Display the price deltas as as string in the radio button. In the product-variant-picker.liquid file, when looping throught the product variants, concatenate the variant name with the variant’s price delta.

{%- for option in product.options_with_values -%}
        
      {%- endfor -%}

I’ve never done this before, but does anyone have any tips or examples I can follow?

I found this liquid guide hepful

I have found the line of code in the theme that modifies the button copy in the file product_variant_option.liquid. Now I need to do some simple math expression like {{ option | minus: option.selected }} to get the delta. If I can really do this on the server side it would be really great and won’t need to add javascript.

It seems that variant.price doesnt work but product.price does?

I wrote out the logic for the “Price Delta” that I want to calculate. The problem is that I don’t know how to refer to the price of the option of that button.

product.price returns the lowest price of the variant, but I want to return the value of the particular option that the button refers to.

What I have so far.

Even if I get this working, it will not update when the user clicks another option. How can I call a javascript function from within the liquid file? Can I put a in the liquid file?

So it seems it is possible to run JS in the liquid theme file, so there may be a way…

The best I can do so far is to put an JS onClick listener that reloads the page when radio buttons are selected. Then the price delta is calculated in liquid (server side).

Now the problem is how to hide selections that are unavailable… like Remote head only comes with maximum 4 detectors. So if the user selects 8 detectors, then clicks “remote head” it will seem really buggy and keep reloading with the previous selection.