1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Coupon Code JS Simple Cart

Discussion in 'JavaScript' started by adamjblakey, Feb 9, 2015.

  1. #1
    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?
    SEMrush
    <div class="cartRow" id="promoCodeDiv">Promo Code: <input type="text" id="code" name="code"><button id="promoSub">Submit</button></div>
    HTML:
    (function(){
        // create a cookie function
        function createCookie(name,value,days) {
            if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
            } else{
                var expires = "";
            }
            document.cookie = name+"="+value+expires+"; path=/";
        }
    
        //This function checks to see if a cookie has been created, if so hide the promo input box
        var getCookie = function(name) {
            var start = document.cookie.indexOf(name + '=');
            if(start < 0){
                return null;
            }
            start = start + name.length + 1;
            var end = document.cookie.indexOf(';', start);
            if(end < 0){
                end = document.cookie.length;
            }
            while(document.cookie.charAt(start) == ' ') {
            start++;
            }
            return unescape(document.cookie.substring(start, end));
        }
    
        //if the cookie exists
        if(getCookie("promo")){
            //the ID for my promo code input box
            jQuery('#promoCodeDiv').hide();
        }  else{
            jQuery('#promoCodeDiv').show();
        }
    
        //This is called when promo code submit button is clicked
        jQuery("#promoSub").click(function Discount() {
            //Interact with PHP file and check for valid Promo Code
            jQuery.post("/includes/discount.php", { code: jQuery('#code').val() } , function(data) {
                console.log(jQuery('#code').val());
                if (data=="0" || data=='') {
                    console.log("Sorry you have entered an incorrect code.");
                }
                else {
                    //create our cookie function if promo code is valid
                    //create cookie for 1 day
                    createCookie("promo","true",1);
                    var y = (data/100);
                    for(var i=0; i<simpleCart.items().length; i++){
                        var itemPrice = simpleCart.items()[i].price();
                        var theDiscount = itemPrice * y;
                        var newPrice = itemPrice - theDiscount;
                        simpleCart.items()[i].set("price", newPrice)
                    }
                    simpleCart.update();
                    //hides the promo box so people cannot add the same promo over and over
                    jQuery('#promoCodeDiv').hide();
                   
                }
            });
        });
    })();
    Code (JavaScript):
     
    adamjblakey, Feb 9, 2015 IP
    SEMrush
  2. jslirola

    jslirola Active Member

    Messages:
    39
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    58
    #2
    Some error? What do you see in the console log?
     
    jslirola, Feb 17, 2015 IP
  3. PDD

    PDD Greenhorn

    Messages:
    67
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #3
    That code won't attach your jquery hooks. Try with this:
    
    jQuery(function(){
        // create a cookie function
        function createCookie(name,value,days) {
            if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
            } else{
                var expires = "";
            }
            document.cookie = name+"="+value+expires+"; path=/";
        }
    
        //This function checks to see if a cookie has been created, if so hide the promo input box
        var getCookie = function(name) {
            var start = document.cookie.indexOf(name + '=');
            if(start < 0){
                return null;
            }
            start = start + name.length + 1;
            var end = document.cookie.indexOf(';', start);
            if(end < 0){
                end = document.cookie.length;
            }
            while(document.cookie.charAt(start) == ' ') {
            start++;
            }
            return unescape(document.cookie.substring(start, end));
        }
    
        //if the cookie exists
        if(getCookie("promo")){
            //the ID for my promo code input box
            jQuery('#promoCodeDiv').hide();
        }  else{
            jQuery('#promoCodeDiv').show();
        }
    
        //This is called when promo code submit button is clicked
        jQuery("#promoSub").click(function Discount() {
            console.log(jQuery('#code').val());
            //Interact with PHP file and check for valid Promo Code
            jQuery.post("/includes/discount.php", { code: jQuery('#code').val() } , function(data) {
              
                console.log(jQuery('#code').val());
                if (data=="0" || data=='') {
                    console.log("Sorry you have entered an incorrect code.");
                }
                else {
                    //create our cookie function if promo code is valid
                    //create cookie for 1 day
                    createCookie("promo","true",1);
                    var y = (data/100);
                    for(var i=0; i<simpleCart.items().length; i++){
                        var itemPrice = simpleCart.items()[i].price();
                        var theDiscount = itemPrice * y;
                        var newPrice = itemPrice - theDiscount;
                        simpleCart.items()[i].set("price", newPrice)
                    }
                    simpleCart.update();
                    //hides the promo box so people cannot add the same promo over and over
                    jQuery('#promoCodeDiv').hide();
                  
                }
            });
        });
    })
    Code (javascript):
     
    PDD, Feb 27, 2015 IP