Advertisement
redearth

Gravity Forms coupon code - jQuery

May 17th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.37 KB | None | 0 0
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.  
  4.     //  when enter coupon code text, update the coupon amount field accordingly
  5.     // 'Allow field to by dynamically populated' must be checked in form field editor
  6.     //  to allow for negative values. no parameters needed.
  7.     $('#input_3_18').bind("input",function(){
  8.  
  9.         var form_id    = 3;              // which form
  10.         var field_id   = 17;             // coupon AMOUNT field
  11.         var coupon     = $(this).val();  // coupon text code field value
  12.         var minlength  = 4;              // minimum length of coupon code before start checking
  13.         var ajaxpath   = 'http://www.yoursite/wp-admin/admin-ajax.php';
  14.         var adminLabel = 'coupon_codes';
  15.  
  16.         if(coupon.length > minlength){
  17.  
  18.             // clear the value of the coupon amount
  19.             $("#ginput_base_price_"+form_id+"_"+field_id).val(0);
  20.             $("span#input_"+form_id+"_"+field_id).html("-$0.00");
  21.  
  22.             // update the form total price
  23.             gformCalculateTotalPrice(form_id);
  24.  
  25.             // indicate that the form is thinking
  26.             $(".ginput_total").append('<span id="loading"> ...validating</span>');
  27.  
  28.             fieldchoices = '';
  29.             // retrieve a list of valid coupon codes, ajax function in functions.php
  30.             $.ajax({
  31.                 url:ajaxpath,
  32.                 type:'POST',
  33.                 data:'action=getchoices_fromfield&adminLabel='+adminLabel+'&form_id='+form_id,
  34.                 success: function(results) {
  35.                     // get an array of choices
  36.                     fieldchoices = $.parseJSON(results);
  37.  
  38.                     $.each(fieldchoices, function(index, obj) {
  39.                         // case insensitive comparison of entered code and valid codes
  40.                         if(coupon.toLowerCase() == obj.text.toLowerCase()){
  41.                             // update the coupon amount input field
  42.                             $("#ginput_base_price_"+form_id+"_"+field_id).val(-obj.value);
  43.                             $("span#input_"+form_id+"_"+field_id).html('-$'+obj.value);
  44.                             // update the form total
  45.                             gformCalculateTotalPrice(form_id);
  46.                         }
  47.                     });
  48.                 }
  49.             });
  50.             $("#loading").fadeOut(5000);
  51.         }
  52.     });
  53. });
  54. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement