Creating a coupon that includes free shipping

davidzupecdavidzupec Member
in Help edited August 2014
Hello, I've created a coupon that would give people who add 1 T-Shirt in their cart a free t-shirt when they insert the coupon code. In the coupon admin area I'm using Discount by a percentage based on the quantity and 1-100 for the details. However I would like them to also get free shipping on that T-Shirt as well, Is that possible to do because I can't find anything regarding shipping when using coupon codes. Thank you!
Tagged:
Comments
  • My apologies I found the snippet and added it to my custom shipping logic : )
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    Glad you found the solution!
  • davidzupecdavidzupec Member
    edited August 2014
    Hey adam, I thought I had it correct, it's seems to be conflicting with my other shipping logic because the free shipping option is showing up now on the checkout form whether I add the coupon "cheers" or not. Here's the code I have, and I placed it at the bottom of the other custom shipping logic. The coupon I'm referring to is Cheers:

    [url]/<![CDATA[

    FC.customLiveShipping = {}

    FC.customLiveShipping.config = {
    autoSelect: true // Set to true if you'd like the top shipping option to be automatically selected for the user
    };

    FC.customLiveShipping.logic = function() {
    /* BEGIN CUSTOM SHIPPING LOGIC */

    var beverages = 0;
    var multiplier = 1
    for ( p in fc_json.products ) {
    switch ( fc_json.products[p].category ) {
    case "BEVERAGE":
    beverages += fc_json.products[p].quantity;
    break;
    }
    }

    if ( beverages > 0 ) {
    multiplier = Math.ceil( beverages / 6 ); // 1-6 > 1, 7-12 > 2, 13-18 > 3, etc
    FC.customLiveShipping.update('all', '+' + ( multiplier * 13 ) );
    FC.customLiveShipping.update(60, 0);
    }

    FC.customLiveShipping.hide(60);

    var zip = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_postal_code").val() : $("#customer_postal_code").val());
    var valid_zips = [11980,11931,11932,11933,11935,11937,11939,11942,11941,11901,11944,11946,11947,11948,11952,11954,11956,11957,11958,11978,11959,11960,11963,11962,1196411360,11436,11427,11420,11378,11420,11378,11428,11421,11421,11429];

    if (jQuery.inArray(parseInt(zip), valid_zips) > -1) {
    var hasCoupon = false;
    if(fc_json.hasOwnProperty('coupons')) {
    jQuery.each(fc_json.coupons, function(i, coupon){
    if(coupon.name.search(/Neighbor/i) > -1) {
    hasCoupon = true;
    }
    });
    }

    if (fc_json.product_count > 5 || hasCoupon) {
    // In valid zips and has more than 5 products or a free ship coupon
    FC.customLiveShipping.hide('all');
    FC.customLiveShipping.show(60);
    }
    }

    if(fc_json.hasOwnProperty('coupons')) {
    jQuery.each(fc_json.coupons, function(i, coupon){
    if(coupon.name.search(/cheers/i) > -1) {
    jQuery("label[for='shipping_service_60']").show();
    jQuery("label[for='shipping_service_60'].fc_shipping_service").text(coupon.name);
    }
    });
    }
    if (jQuery("label[for='shipping_service_60']").is(":hidden")) {
    jQuery("label[for='shipping_service_60']").remove();
    }
    });

    /* END CUSTOM SHIPPING LOGIC */
    }

    //]]>
    [/url]
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    I think I helped you recently with getting coupons added? You can actually alter the existing code to add in the new coupon - so you would remove that new block you added, and instead modify what's already there - so your custom logic would look like this:
    /* BEGIN CUSTOM SHIPPING LOGIC */
    
    var beverages = 0;
    var multiplier = 1
    for ( p in fc_json.products ) {
    switch ( fc_json.products[p].category ) {
    	case "BEVERAGE":
    		beverages += fc_json.products[p].quantity;
    		break;
    	}
    }
    
    if ( beverages > 0 ) {
    	multiplier = Math.ceil( beverages / 6 ); // 1-6 > 1, 7-12 > 2, 13-18 > 3, etc
    	FC.customLiveShipping.update('all', '+' + ( multiplier * 13 ) );
    	FC.customLiveShipping.update(60, 0);
    }
    
    FC.customLiveShipping.hide(60);
    
    var zip = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_postal_code").val() : $("#customer_postal_code").val());
    var valid_zips = [11980,11931,11932,11933,11935,11937,11939,11942,11941,11901,11944,11946,11947,11948,11952,11954,11956,11957,11958,11978,11959,11960,11963,11962,1196411360,11436,11427,11420,11378,11420,11378,11428,11421,11421,11429];
    
    if (jQuery.inArray(parseInt(zip), valid_zips) > -1) {
    	var hasCoupon = false;
    	if(fc_json.hasOwnProperty('coupons')) {
    		jQuery.each(fc_json.coupons, function(i, coupon){
    			if(coupon.name.search(/Neighbor/i) > -1 || coupon.name.search(/cheers/i) > -1) {
    				hasCoupon = true;
    			}
    		});
    	}
    
    	if (fc_json.product_count > 5 || hasCoupon) {
    		// In valid zips and has more than 5 products or a free ship coupon
    		FC.customLiveShipping.hide('all');
    		FC.customLiveShipping.show(60);
    	}
    }
    
    /* END CUSTOM SHIPPING LOGIC */
    
  • Hey Adam, thank you! but the thing is this new coupon (cheers) is not restricted to those zip codes above, the free shipping should be applied to anyone using that coupon code Cheers. I wasn't sure how to implement that?
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    Ah ok - no worries. Did you want only free shipping displayed for 'cheers' too?
  • Hey Adam, yeah what I was looking to have was any customer who purchases a t-shirt and entered Cheers for the coupon would get it free and also receive free shipping on that item. I already set up the coupon to deduct the cost of the shirt, so now my issue was how to implement the free shipping for them. Thank you!
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    Ok awesome - try this:
    /* BEGIN CUSTOM SHIPPING LOGIC */
     
    var beverages = 0;
    var multiplier = 1
    for ( p in fc_json.products ) {
    switch ( fc_json.products[p].category ) {
    	case "BEVERAGE":
    		beverages += fc_json.products[p].quantity;
    		break;
    	}
    }
     
    if ( beverages > 0 ) {
    	multiplier = Math.ceil( beverages / 6 ); // 1-6 > 1, 7-12 > 2, 13-18 > 3, etc
    	FC.customLiveShipping.update('all', '+' + ( multiplier * 13 ) );
    	FC.customLiveShipping.update(60, 0);
    }
     
    FC.customLiveShipping.hide(60);
     
    var zip = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_postal_code").val() : $("#customer_postal_code").val());
    var valid_zips = [11980,11931,11932,11933,11935,11937,11939,11942,11941,11901,11944,11946,11947,11948,11952,11954,11956,11957,11958,11978,11959,11960,11963,11962,1196411360,11436,11427,11420,11378,11420,11378,11428,11421,11421,11429];
     
    if (jQuery.inArray(parseInt(zip), valid_zips) > -1) {
    	var hasCoupon = false;
    	if(fc_json.hasOwnProperty('coupons')) {
    		jQuery.each(fc_json.coupons, function(i, coupon){
    			if(coupon.name.search(/Neighbor/i) > -1) {
    				hasCoupon = true;
    			}
    		});
    	}
     
    	if (fc_json.product_count > 5 || hasCoupon) {
    		// In valid zips and has more than 5 products or a free ship coupon
    		FC.customLiveShipping.hide('all');
    		FC.customLiveShipping.show(60);
    	}
    }
    
    if(fc_json.hasOwnProperty('coupons')) {
    	jQuery.each(fc_json.coupons, function(i, coupon){
    		if(coupon.name.search(/cheers/i) > -1) {
    			// Has a coupon that gets free shipping
    			FC.customLiveShipping.hide('all');
    			FC.customLiveShipping.show(60);
    		}
    	});
    }
     
    /* END CUSTOM SHIPPING LOGIC */
    
  • Hmmm... unfortunately that didn't work. I still got the live shipping rate options displayed.
  • Hey Adam, it worked only for those restricted zip codes though?
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    Ah I see what's happening. The coupon code is 'cheers', but what you actually need to put within the code there is part of the coupon name. So in your case, you could put:
    if(coupon.name.search(/free t-shirt/i) > -1) {
    
  • Hey adam, awesome that worked, thank you! So the next time I want to add another coupon I have to add the coupon name and not the code then?
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    That's correct. And by using the name and not the code, it prevents anyone being able to see what the coupon code is from your checkout's source code.
  • Oh I see, I'll have to remember that! Thank you again for your help on this, very much appreciated Adam!
Sign In or Register to comment.