Skip to content

Instantly share code, notes, and snippets.

@amacneil
Created March 29, 2012 04:12
Show Gist options
  • Save amacneil/2233226 to your computer and use it in GitHub Desktop.
Save amacneil/2233226 to your computer and use it in GitHub Desktop.
How to update Store order totals via AJAX
{exp:store:checkout form_id="checkout_main" next="checkout/confirm"}
<table class="table">
<thead>
<tr>
<th style="width:60%">Product</th>
<th>Price</th>
<th>Qty</th>
<th style="text-align:right">Total</th>
</tr>
</thead>
<tbody>
{items}
<tr>
<td>{title}</td>
{if entry_id == 215}
<td>${item_qty}.00</td>
<td>1</td>
{if:else}
<td>{price}</td>
<td><input name="items[{key}][item_qty]" value="{item_qty}" class="input-mini" /></td>
{/if}
<td style="text-align:right">{item_subtotal}</td>
</tr>
{/items}
</tbody>
<tbody id="checkout_totals">
{checkout_totals_snippet}
</tbody>
</table>
<p>{field:billing_country}</p>
<p><input type="submit" name="update_totals" value="Update Totals" /></p>
{/exp:store:checkout}
<script type="text/javascript">
$("#billing_country").change(function() {
// submit the form data via AJAX
$.post('/checkout/update', $(this.form).serialize()).done(function(response) {
// update totals
var response_html = $(response);
$('#checkout_totals').replaceWith(response_html.find('#checkout_totals'));
// update XID so it will work more than once
$('#checkout_main').find('input[name="XID"]').val(response_html.find('input[name="XID"]').val());
});
});
</script>
<tr>
<th colspan="3">Subtotal</th>
<th style="text-align:right">{order_subtotal}</th>
</tr>
<tr>
<td colspan="3" class="{if error:promo_code}error{/if}" style="vertical-align: middle">
Promo Code &nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="promo_code" value="{promo_code}" class="input-medium" />
{error:promo_code}
</td>
<td style="text-align:right; vertical-align: middle;">{order_discount}</td>
</tr>
{if tax_name}
<tr>
<td colspan="3">{tax_name} ({tax_percent}%)</td>
<td style="text-align:right">{order_tax}</td>
</tr>
{/if}
<tr>
<th colspan="3">Total</th>
<th style="text-align:right">{order_total}</th>
</tr>
{exp:store:checkout}
<table>
<tbody id="checkout_totals">
{checkout_totals_snippet}
</tbody>
</table>
{/exp:store:checkout}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment