1

I'm trying to modify a cart of a shopify store. It's using a bundle application, which overwrites the prices in the cart, hence I'm trying to get those overwrited prices and calculate with those, in order to be able to display how much the customers are saving with the discount. My code is like this:

<script>
          const original_price_for_product_range = document.getElementsByClassName("revy-line-item-price__old");
          const a = original_price_for_product_range[0].outerText;
          const c = a.replace("€","");
          const d = c.replace(",",".");
          const old_price_for_product = +d;
          
          const discounted_price_for_product_range = document.getElementsByClassName("revy-line-item-price__new");
          const b = discounted_price_for_product_range[0].outerText;
          const f = b.replace("€","");
          const g = f.replace(",",".");
          const new_price_for_product = +g
</script>

Basically I'm getting the old and new price by their class name and then getting the first element out of that array and converting it to a number, I can calculate with. (outer text to get the first price, then removing the € and then replacing , with . so I can convert the text to a number, which I can do calculations with).

My problem is that after getting the elements by their class name, ALL following process just WON'T run, getting "Uncaught ReferenceError: a is not defined at <anonymous>:1:1" errors.

I was expecting the code to get the array first element and convert it to number given the script's code, but it's not working. Can anyone help out with this?

  • Check the outerHTML of original_price_for_product_range[0]; what kind of an element is it? Could it be a void element? – Joel Peltonen Dec 23 '22 at 22:15
  • It's actually was not a void element, so that's why I run into trouble. – Strikementos Jan 04 '23 at 13:08
  • I mean if it _was_ a void element then it would make sense that innerHTML could be null, not the other way around :) So did you figure it out? And you didn't say what element is it? Different elements might implement "innerHTML" in different ways, think of for example a textarea – Joel Peltonen Jan 05 '23 at 09:10
  • What I find working is delaying the function of getting the range[0] element and everything afterwards since I found that when I delay it, it can gather the correct data. I assume the problem was that it didn't load properly and wanted to calculate with a not loaded data. – Strikementos Jan 06 '23 at 12:19

0 Answers0