Coupon Code JS Simple Cart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    Coupon Code JS Simple Cart

    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?

    Code:
    <div class="cartRow" id="promoCodeDiv">Promo Code: <input type="text" id="code" name="code"><button id="promoSub">Submit</button></div>
    Code:
    (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();
                   
                }
            });
        });
    })();
    Thanks in advance.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    In the first code segment, you fail to specify the type of control button is. See bold below
    Code:
    <div 
      class="cartRow" 
      id="promoCodeDiv"
    >
      Promo Code: 
      <input 
        type="text" 
        id="code" 
        name="code"
      >
      <button 
        [B]type="button"[/B]
        id="promoSub"
      >
        Submit
      </button>
    </div>
    In part two you say "//This is called when promo code submit button is clicked
    jQuery("#promoS ub").click(func tion 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.

    Comment

    • katherineavile
      New Member
      • Jan 2022
      • 1

      #3
      A promo code, discount code, or voucher code is another name for a coupon code. Promo codes are similar to printed coupon codes in that they allow you to save money by receiving a promotional discount on a specific or complete product range. Retailers may gain loyal consumers and enhance sales by using coupon codes. Making a greeting card body> in HTML Card headers are being added. div class="Header"> div class="Header"> div class="Header"> div class=" Adding product information. div class="Cart-Items"> HTML: Putting together a counter. Adding a pricing section in HTML. Creating a checkout RevealCode section in HTML. HTML:

      Comment

      • Abfd
        Banned
        New Member
        • Nov 2022
        • 2

        #4
        I got the best questions that are in demand from JavaScript one of the most languages out there and if you can solve these questions you can get your job, but if you can't they provide you free courses step by step it depends on your level in order to become a programmer and good luck.. Learn more: https://javascript.spread.name/

        Comment

        Working...