5 Ways Digital Marketers Use Call Tracking to Grow Their Business
Blog /Optimization

JavaScript for Optimizers: Inserting a Total Discount

23rd Jun 2021 –

JavaScript for Optimizers is a series of blog posts that teach you how to optimize your code, from start to finish.

In this post, we will cover one of the most important optimization techniques: inserting a total discount on your page.

Why do you need one, you ask?

Well, you can’t go wrong with adding an attractive and informative “total discount” section on your site. It’s not only good for conversions but also great for helping your customer see just how much they can save shopping with you.

Using the existing prices on the page, we can calculate the total added value for the customer. This can then be displayed underneath the other prices.

Business Case: Inserting a Total Discount

Here’s what the end result is going to look like:

Adding the total discount of all products
Adding the total discount of all products

The Code

Did the illustration above pique your interest?

Follow the step-by-step instructions below to execute the change.

First, run the following JavaScript code on the testing page. Try it out for yourself to see it in action!

var prices = document.querySelectorAll(".productprice");
var totalPrice = document.querySelector(".totalprice");
var discount = 0;
for (var i = 0; i < prices.length; i++) {
  var cutPrices = prices[i].innerText.split(" ");
  var price = Number(cutPrices[3]);
  var specialprice = Number(cutPrices[5]);
  var discount = discount + (price - specialprice);
}
var totalPriceElement = document.createElement("p");
var totalPriceElementNode = document.createTextNode(
  "Total discount: €" + discount + ".00"
);
totalPriceElement.style.color = "green";
totalPriceElement.style.fontWeight = "bold";
totalPriceElement.appendChild(totalPriceElementNode);
totalPrice.prepend(totalPriceElement);

The Logic

var prices = document.querySelectorAll(".productprice");

We start of by assigning all the product prices with the class “productprice” to a variable.

var totalPrice = document.querySelector(".totalprice");

Then, assign the current totalprice to the totalPrice variable.

var discount = 0;

We need to declare a discount variable that starts at 0 so we can add a number every time the loop runs.

for (var i = 0; i < prices.length; i++) {

Then, start a loop that runs the length of the prices array we assigned earlier.

var cutPrices = prices[i].innerText.split(" ");

Save the entire price string in the array. This means that we need to split the string for the part that we need. This will split the string into different parts and save it into an array.

var price = Number(cutPrices[3]);

At index 3 of this array, we find the price before the discount. Immediately convert this to a number.

var specialprice = Number(cutPrices[5]);

At index 5 of this array, we find the price after the discount. Once again, immediately convert this to a number.

var discount = discount + (price - specialprice);

Then, calculate the price difference between those prices and add it to the discount variable. This loop will end when we have the total discount added.

}

For the next step, close out the loop.

var totalPriceElement = document.createElement("p");

Next, we need a new paragraph element in order to append our string with the correct formatting.

var totalPriceElementNode = document.createTextNode(
"Total discount: €" + discount + ".00"
);

This paragraph will contain a text node with the text we want to show.

totalPriceElement.style.color = "green";

Give the paragraph a green color to signal a positive number.

totalPriceElement.style.fontWeight = "bold";

Add a bold font weight, to make it stand out more.

totalPriceElement.appendChild(totalPriceElementNode);

Then, add the textnode to our paragraph element.

totalPrice.prepend(totalPriceElement);

Finally, add our total discount to the DOM.

Good luck with testing! Check out the previous episode if you want to learn how to add a back-to-top button. You can also follow me on LinkedIn for weekly content about A/B testing with JavaScript.

Start 15-Day Free Trial Of Convert Experiences.

Quick form submission
Quick submission.
No credit card needed
No credit card needed.
You can always change your preferences later.
Check Icon
You're Almost Done.
Managing Marketing Team
Managing Tech Team
Hypothesizing Experiments
Coding and QA of Experiments
Convert is committed to protecting your privacy.

You've successfully submitted your details to start your Convert Experiences 15-day free trial.

We are setting up your account for you. You'll receive a welcome message to your registered email with your login access. Please explore the app on desktop for an optimal experience.