I'm writing a checkout form for purchasing online tests. You can buy a certification or a recertification, as well as a pdf or book study manual.
I'm using Jinja2 templates to provide test data into the Javascript. The test object is a dictionary with the following structure. All prices are in cents.
test {
id: Integer,
name: String,
acronym: String,
certification_price: Integer,
recertification_price: Integer,
study_pdf_price: Integer,
study_book_price: Integer,
}
If a user buys more than one test, they get a discount of 10% off for each additional test.
<!doctype html>
<html>
<head>
<script>
$(document).ready(function() {
var calcTotal = function(){
var total = 0;
var numTests = 0;
{% for test in tests %}
{% if test.certification_price %}
if ($('#buy-{{ test.acronym }}-certification').is(':checked')) {
total += {{ test.certification_price }};
numTests++;
}
{% endif %}
{% if test.recertification_price %}
if ($('#buy-{{ test.acronym }}-recertification').is(':checked')) {
total += {{ test.recertification_price }};
numTests++;
}
{% endif %}
{% if test.study_pdf_price %}
if ($('#buy-{{ test.acronym }}-pdf').is(':checked')) {
total += {{ test.study_pdf_price }};
}
{% endif %}
{% if test.study_book_price %}
if ($('#buy-{{ test.acronym }}-book').is(':checked')) {
total += {{ test.study_book_price }};
}
{% endif %}
{% endfor %}
// take 10 percent off for each additional test after the first
if (numTests > 0) numTests--;
var discountPrice = Math.round(((10 - numTests) * 0.1) * total);
$('.product-total').html(Math.round(discountPrice));
$('#final-total').val(discountPrice);
}
$('.product-selection').click(calcTotal);
calcTotal();
});
</script>
<body>
<h2>Selected Products</h2>
<form id="registration-form" action="" method="POST">
<table id="products-table">
<tr>
<th>Product</th>
<th>Price</th>
<th>Add to Cart</th>
</tr>
{% for test in tests %}
{% if test.certification_price %}
<tr>
<td>{{ test.acronym }} Certification{% if test.study_pdf_price %} (PDF Manual Included){% endif %}</td>
<td>${{ test.certification_price }}</td>
<td><input class="product-selection" type="checkbox" id="buy-{{ test.acronym }}-certification" name="buy-{{ test.acronym }}-certification" /></td>
</tr>
{% endif %}
{% if test.recertification_price %}
<tr>
<td>{{ test.acronym }} Recertification</td>
<td>${{ test.recertification_price }}</td>
<td><input class="product-selection" type="checkbox" id="buy-{{ test.acronym }}-recertification" name="buy-{{ test.acronym }}-recertification" /></td>
</tr>
{% endif %}
{% if test.study_pdf_price %}
<tr>
<td>{{ test.acronym }} {{ test.study_pdf_description }}</td>
<td>${{ test.study_pdf_price }}</td>
<td><input class="product-selection" type="checkbox" id="buy-{{ test.acronym }}-pdf" name="buy-{{ test.acronym }}-pdf" /></td>
</tr>
{% endif %}
{% if test.study_book_price %}
<tr>
<td>{{ test.acronym }} {{ test.study_book_description }}</td>
<td>${{ test.study_book_price }}</td>
<td><input class="product-selection" type="checkbox" id="buy-{{ test.acronym }}-book" name="buy-{{ test.acronym }}-book" /></td>
</tr>
{% endif %}
<tr><td> </td><td> </td><td> </td></tr>
{% endfor %}
<tr><td><strong>Total</strong></td><td><strong>$<span class="product-total">0</span></strong></td></tr>
</table>
<input type="hidden" id="final-total" name="final-total" />
<p><input id="submit-button" type="submit" value="Place Order"></p>
</form>
</body>
</html>
I feel like I am repeating myself a lot, but I don't know how I would do this differently. Also, this way of providing data by writing Javascript code with the templating language seems messy. Any ideas?