Show Amount Saved on Product (for products with Variants)

I am trying to show the dollar amount saved on the Product info, when using Shopify’s Compare at Price Feature.

I have used the code from https://community.shopify.com/post/1134889 to implement this, and it has worked for all of my products with just one variant.

However when I add in 2 or more variants, then the calculation breaks and the wrong figure is shown. This resolves itself when I delete the second variant. You can see that the Superbly Stainless option (which has two variants) has the incorrect saving calculation, while the other two products which only have 1 product variant have the right saving calculation:

This issue occurs on all pages where the product card shows, including on the product page iteself.

Can anyone suggest how to adapt this code so that it works to correctly display the saving on products with multiple variants?

Hey, did you ever find an answer for this?