By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Coupon Code JS Simple Cart

100+
P: 133
Hi,

I am trying to add this to my website which is a promo code script which connects to JS simple cart but doesn't seem to be working and does not even create an action like the JS is being triggered. Can anyone see why this is not running the function?

Expand|Select|Wrap|Line Numbers
  1. <div class="cartRow" id="promoCodeDiv">Promo Code: <input type="text" id="code" name="code"><button id="promoSub">Submit</button></div>
  2.  
Expand|Select|Wrap|Line Numbers
  1. (function(){
  2.     // create a cookie function
  3.     function createCookie(name,value,days) {
  4.         if (days) {
  5.         var date = new Date();
  6.         date.setTime(date.getTime()+(days*24*60*60*1000));
  7.         var expires = "; expires="+date.toGMTString();
  8.         } else{
  9.             var expires = "";
  10.         }
  11.         document.cookie = name+"="+value+expires+"; path=/";
  12.     }
  13.  
  14.     //This function checks to see if a cookie has been created, if so hide the promo input box
  15.     var getCookie = function(name) {
  16.         var start = document.cookie.indexOf(name + '=');
  17.         if(start < 0){
  18.             return null;
  19.         }
  20.         start = start + name.length + 1;
  21.         var end = document.cookie.indexOf(';', start);
  22.         if(end < 0){
  23.             end = document.cookie.length;
  24.         }
  25.         while(document.cookie.charAt(start) == ' ') {
  26.         start++;
  27.         }
  28.         return unescape(document.cookie.substring(start, end));
  29.     }
  30.  
  31.     //if the cookie exists
  32.     if(getCookie("promo")){
  33.         //the ID for my promo code input box
  34.         jQuery('#promoCodeDiv').hide();
  35.     }  else{
  36.         jQuery('#promoCodeDiv').show();
  37.     }
  38.  
  39.     //This is called when promo code submit button is clicked
  40.     jQuery("#promoSub").click(function Discount() {
  41.         //Interact with PHP file and check for valid Promo Code
  42.         jQuery.post("/includes/discount.php", { code: jQuery('#code').val() } , function(data) {
  43.             console.log(jQuery('#code').val());
  44.             if (data=="0" || data=='') {
  45.                 console.log("Sorry you have entered an incorrect code.");
  46.             }
  47.             else {
  48.                 //create our cookie function if promo code is valid
  49.                 //create cookie for 1 day
  50.                 createCookie("promo","true",1);
  51.                 var y = (data/100);
  52.                 for(var i=0; i<simpleCart.items().length; i++){
  53.                     var itemPrice = simpleCart.items()[i].price();
  54.                     var theDiscount = itemPrice * y;
  55.                     var newPrice = itemPrice - theDiscount;
  56.                     simpleCart.items()[i].set("price", newPrice)
  57.                 }
  58.                 simpleCart.update();
  59.                 //hides the promo box so people cannot add the same promo over and over
  60.                 jQuery('#promoCodeDiv').hide();
  61.  
  62.             }
  63.         });
  64.     });
  65. })();
  66.  
Thanks in advance.
Feb 13 '15 #1
Share this Question
Share on Google+
1 Reply


Claus Mygind
100+
P: 571
In the first code segment, you fail to specify the type of control button is. See bold below
Expand|Select|Wrap|Line Numbers
  1. <div 
  2.   class="cartRow" 
  3.   id="promoCodeDiv"
  4. >
  5.   Promo Code: 
  6.   <input 
  7.     type="text" 
  8.     id="code" 
  9.     name="code"
  10.   >
  11.   <button 
  12.     type="button"
  13.     id="promoSub"
  14.   >
  15.     Submit
  16.   </button>
  17. </div>
  18.  
In part two you say "//This is called when promo code submit button is clicked
jQuery("#promoSub").click(function Discount() "

I do not use jQuery, but I don't see where in your code you assign any action to the promo Submit button. You would either have to put in an "onClick" event on the button or add and eventListner for some action to occur.
Feb 20 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.