Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. <!-- Give the purchaser a discount if the correct discount code is entered into
  2. the storefront checkout page. You define the discount code and the discount.
  3. The discount code cannot have quotation marks. The discount rate is a decimal
  4. number. For example, an discount of 15% is 0.15. -->
  5.  
  6. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  7. <script type="text/javascript">
  8.  
  9. // Discount rate is 15% if the discount code is "Mountain Goat".
  10. var DISCOUNT_CODE = "Mountain Goat";
  11. var DISCOUNT_RATE = 0.15;
  12.  
  13. $(document).ready(function() {
  14. // This script is restricted to storefront checkout pages.
  15. if (RegExp('checkOut.jsp\\?storefront_KEY=').test(window.location.href)) {
  16.  
  17. // Update the total by getting the page shipping charge and
  18. // formatting the subtotal, shipping and total fields.
  19. function updateTotal() {
  20. var sub = parseFloat($('*[name=sub]').val());
  21.  
  22. var discount = 0.0;
  23. var discountCode = $('#discount_code').val();
  24. if (discountCode.length > 0 && discountCode == DISCOUNT_CODE) {
  25. discount = -1 * sub * DISCOUNT_RATE;
  26. }
  27. if (discount != 0.0) {
  28. $('#discount').val(discount.toFixed(2));
  29. } else {
  30. $('#discount').val('');
  31. }
  32.  
  33. var shipping = parseFloat($('[name=shipping]').val());
  34. shipping = shipping !== null ? shipping : 0;
  35. $('*[name=shipping]').val(shipping.toFixed(2));
  36.  
  37. var amountDisplay = sub + shipping + discount;
  38. $('*[name=amountDisplay]').val(amountDisplay.toFixed(2));
  39.  
  40. // Just A Number. Cannot be fancy.
  41. $('*[name=amount]').val(amountDisplay.toFixed(2));
  42. }
  43.  
  44. // Add the discount code and amount fields.
  45. $('td:contains("Shipping/Handling")').filter(':last').parent().before($('#discount_row'));
  46. $('#apply_discount').click(updateTotal);
  47. }
  48. });
  49. </script>
  50. <!-- Style changes to make the totals look better. -->
  51. <style type="text/css">
  52. /* Make the subtotals and totals right-justified. */
  53.  
  54. #sub,
  55. #discount,
  56. #shipping,
  57. #amountDisplay {
  58. padding-right: 15px;
  59. text-align: right;
  60. }
  61.  
  62. #discount_code {
  63. font-family: Droid Sans;
  64. font-size: 11pt;
  65. text-align: left;
  66. }
  67.  
  68. td input {
  69. text-align: right;
  70. }
  71. #shoppingCart a {
  72. font-size: 18pt;
  73. font-weight: 600;
  74. font-family: Arial;
  75. color: forestgreen;
  76. padding-left: 20px;
  77. padding-right: 20px;
  78. }
  79. </style>
  80. <div style="display:none">
  81. <table>
  82. <thead/>
  83. <tbody>
  84. <tr id="discount_row">
  85. <td></td>
  86. <td>Discount code:
  87. <input size="14" id="discount_code">
  88. </td>
  89. <td align="right">Discount:</td>
  90. <td align="right">
  91. $
  92. <input id="discount" value="" size="7" readonly>
  93. </td>
  94. <td>
  95. <input type="button" value="Apply" id="apply_discount" />
  96. </td>
  97. <td> </td>
  98. </tr>
  99. </tbody>
  100. </table>
  101. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement