Checking for a coupon in the cart when the cart is empty

xcorlettxcorlett Member
in Help edited November 2012
I'm having a problem with the following script that I am running from all of my store pages. It requests the cart contents, and checks for the existence of a coupon. If one does not exist, it returns "undefined" and shows the "add a coupon" box. The problem I am having is when someone activates the coupon before they have a product in the cart, the coupon is applied but the script still returns an "undefined", thus causing the coupon box to still appear. I'm using v1.
<script type="text/javascript">
jQuery(document).ready(function(){
	setTimeout(function() {	
		jQuery.getJSON('https://'+storedomain+'/cart?'+fcc.session_get()+'&output=json&callback=?', function() {		
           if (typeof(FC.json.coupons) == "undefined") {
         			$("#specials").html('November Special - 10% off all orders throughout November - click this box to activate it');
         		    $("#specials").fadeIn(); 
         		    }
        				})
        	}, 1500);
});
</script>

Any ideas?

Thanks!
Robert
Comments
  • fc_adamfc_adam FoxyCart Team
    @xcorlett,

    Could you link us to a page that we could test this out please? I'd love to take a look at this in action
  • xcorlettxcorlett Member
    edited November 2012
    Absolutely, right here: newstarnootropics.com
  • For anyone else who is interested, here is the related piece of code that quietly adds the coupon to the cart when the box is clicked:
    $(document).ready(function(){
      $("#specials").click(function(e){						
      	jQuery.getJSON('https://'+storedomain+'/cart?'+fcc.session_get()+'&coupon=NEWSTAR10&output=json&callback=?');
          e.preventDefault(); // 
              _gaq.push(['_trackPageview', '/couponadded.html']); 
    		fcc.cart_update.call(fcc);
           			$("#specials").css('font-weight','bold');
    				$("#specials").html('Success! Your 10% off coupon has been activated.');		
    		        $("#specials").effect("shake", { times:3 }, 500).delay(2000);
          		    $("#specials").fadeOut();
    	});
    });
    
  • fc_adamfc_adam FoxyCart Team
    edited November 2012
    @xcorlett,

    Ok - so the issue you're hitting is you're adding the cart to the session, but you're not actually updating the local JSON object with the new data - so in your javascript block that checks if it's set, it doesn't actually no. I'd hazard a guess that if you refresh the page, your javascript will see the coupon in the session.

    What you'll want to do is actually setup the callback function for your coupon call to get the returned data:
    $(document).ready(function(){
    	$("#specials").click(function(e){						
    		jQuery.getJSON('https://'+storedomain+'/cart?'+fcc.session_get()+'&coupon=NEWSTAR10&output=json&callback=?', function(data) {
    			FC.json = data;
    			fcc.cart_update.call(fcc);
    			$("#specials").css('font-weight','bold');
    			$("#specials").html('Success! Your 10% off coupon has been activated.');		
    			$("#specials").effect("shake", { times:3 }, 500).delay(2000);
    			$("#specials").fadeOut();
    		});
    		e.preventDefault(); // 
    		_gaq.push(['_trackPageview', '/couponadded.html']); 
    		
    	});
    });
    


    Similar with your other block, you'll want to reference what's returned from the JSON call - not the local object:
    jQuery(document).ready(function(){
    	setTimeout(function() {	
    		jQuery.getJSON('https://'+storedomain+'/cart?'+fcc.session_get()+'&output=json&callback=?', function(data) {		
    			if (typeof(data.coupons) == "undefined") {
    				$("#specials").html('November Special - 10% off all orders throughout November - click this box to activate it');
    				$("#specials").fadeIn(); 
    			}
    		});
    	}, 1500);
    });
    
  • Adam,
    Thank-you so much! That is more than helpful and should help me with future foxy scripts.
    Robert
Sign In or Register to comment.