Updatd to 2.0 but effected live shipping rates

davidzupecdavidzupec Member
in Help edited October 2014
Hello I updated to the 2.0 version, I love it! My issue is that only certain zip codes should receive free shipping. Whats happening now is everyone is seeing the free shipping option. I'm not sure how to fix this for this version? My code is below with the latest live rates snippet. Thank you!
{% if context == 'cart' or context == 'checkout' %}
<script type="text/javascript" charset="utf-8">
    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,11964,11965,11966,11970,11968,11971,11972,11792,11975,11976,11977,10026,10027,10030,10037,10039,10001,10011,10018,10019,10020,10036,10029,10035,10010,10016,10017,10022,10012,10013,10014,10004,10005,10006,10007,10038,10280,10002,10003,10009,10021,10028,10044,10138,10023,10024,10025,10031,10032,10033,10034,10040,11930,11701,11708,11931,11768,11702,11703,11704,11707,11933,11743,11963,11706,11751,11743,11705,11930,11743,11777,11713,11715,11716,11780,11717,11932,11718,11719,11933,11772,11702,11980,11934,11720,11721,11722,11749,11760,11782,11724,11743,11725,11726,11727,11770,11768,11935,11772,11729,11780,11746,11937,11730,11939,11940,11731,11772,11942,11733,11967,11941,11768,11717,11731,11706,11738,11770,11782,06390,11901,11780,11768,11702,11763,11739,11740,11944,11743,11946,11743,11749,11760,11788,11780,11757,11954,11741,11742,00501,00544,11743,11743,11746,11747,11750,11749,11760,11751,11751,11752,11947,11754,11706,11743,11755,11961,11779,11779,11948,11757,11743,11743,11950,11949,11726,11950,11951,11952,11763,11747,11750,11775,11805,11953,11764,11954,11955,11766];
		 
		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 > 0 || hasCoupon) {
		    // In valid zips and 1 product or more 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(/Free T-Shirt/i) > -1) {
					// Has a coupon that gets free shipping
					FC.customLiveShipping.hide('all');
					FC.customLiveShipping.show(60);
				}
			});
		}				
 
 
 
    /* END CUSTOM SHIPPING LOGIC */
    };
 
    /* Live Rate Shipping Modification Logic v2.0.3 */
    FC.customLiveShipping.add=function(a,b,d,c){FC.json.shipping_address.shipping_results.push({method:d,price:b,service_id:a,service_name:c})};FC.customLiveShipping.hide=function(a){a=FC.customLiveShipping.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.json.shipping_address.shipping_results[a[b]].hide=!0};FC.customLiveShipping.show=function(a){a=FC.customLiveShipping.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.json.shipping_address.shipping_results[a[b]].hide=!1};
    FC.customLiveShipping.update=function(a,b,d,c){a=FC.customLiveShipping.filterShippingOptions(a);for(var e=0;e<a.length;e++){if("number"===typeof b||"string"===typeof b&&""!==b){var f=FC.customLiveShipping.modifyPrice(FC.json.shipping_address.shipping_results[a[e]].price,b);FC.json.shipping_address.shipping_results[a[e]].price=f}"string"===typeof d&&(FC.json.shipping_address.shipping_results[a[e]].method=d);"string"===typeof c&&(FC.json.shipping_address.shipping_results[a[e]].service_name=c)}};
    FC.customLiveShipping.remove=function(a){FC.customLiveShipping.hide(a)};FC.customLiveShipping.reset=function(){FC.cart.getShippingOptions({address:FC.json.shipping_address})};function customLiveShippingHandler(){FC.customLiveShipping.execute()}function customCouponHandler(){FC.customLiveShipping.reset()}
    FC.customLiveShipping.execute=function(){if(0<FC.json.shipping_address.shipping_results.length&&(!FC.json.shipping_address.hasOwnProperty("customLiveShippingHash")||FC.json.shipping_address.customLiveShippingHash!=FC.util.hashString(JSON.stringify(FC.json.shipping_address.shipping_results)))){FC.customLiveShipping.logic();for(var a=FC.json.shipping_address.shipping_results.length-1;0<=a;a--)FC.json.shipping_address.shipping_results[a].hide&&FC.json.shipping_address.shipping_results.splice(a,1);FC.customLiveShipping.config.autoSelect&&
    0==FC.json.shipping_address.shipping_service_id&&(FC.json.shipping_address.shipping_service_id=FC.json.shipping_address.shipping_results[0].service_id,FC.json.show_shipping_tbd=!1);FC.Template(FC.json.context).clearOutput();FC.cart.recalculateCartTotals();FC[FC.json.context].renderShippingRates(FC.json.shipping_address);FC.json.shipping_address.customLiveShippingHash=FC.util.hashString(JSON.stringify(FC.json.shipping_address.shipping_results))}};
    FC.customLiveShipping.filterShippingOptions=function(a){if("number"==typeof a)for(var b=0;b<FC.json.shipping_address.shipping_results.length;b++){if(FC.json.shipping_address.shipping_results[b].service_id==a)return[b]}else{if("string"==typeof a){for(var d=[],c={},b=0;b<FC.json.shipping_address.shipping_results.length;b++){var e=FC.json.shipping_address.shipping_results[b];c[b]=e.method+" "+e.service_name}if("all"!=a.toLowerCase()){a=/(fedex|usps|ups)?\s?([\w\s]+)?/i.exec(a);if(void 0==a)return;for(b in c)void 0!=
    a[1]&&-1==c[b].toLowerCase().indexOf(a[1].toLowerCase())?delete c[b]:void 0!=a[2]&&-1==c[b].toLowerCase().indexOf(a[2].toLowerCase())&&delete c[b]}for(b in c)d.push(parseInt(b));return d}if("object"==typeof a){d=[];for(b=0;b<a.length;b++)for(c=0;c<FC.json.shipping_address.shipping_results.length;c++)FC.json.shipping_address.shipping_results[c].service_id==a[b]&&d.push(c);return d}}};
    FC.customLiveShipping.modifyPrice=function(a,b){b=b.toString();var d=/([\+\-\=\*\/])?(\d+(?:\.\d+)?)(\%)?/.exec(b);a=parseFloat(a);var c=parseFloat(d[2]);void 0!=d[3]&&(c=c/100*a);switch(void 0==d[1]?"=":d[1]){case "+":a+=c;break;case "-":a-=c;break;case "/":a/=c;break;case "*":a*=c;break;default:a=c}return 0>a?0:a};FC.client.on("cart-shipping-options-update.done",customLiveShippingHandler);FC.client.on("checkout-shipping-options-update.done",customLiveShippingHandler);
    FC.client.on("customer-login.done",customLiveShippingHandler);FC.client.on("cart-coupon-add.done",customCouponHandler);FC.client.on("cart-coupon-remove.done",customCouponHandler);
    {% if cart_is_fullpage or context == 'checkout' %}FC.client.on("ready.done",customLiveShippingHandler);{% else %}customLiveShippingHandler();{% endif %}
</script>
{% endif %}
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    Give this a try:
    /* BEGIN CUSTOM SHIPPING LOGIC */
     
     var beverages  = 0;
        var multiplier = 1
        for ( var p = 0; = < FC.json.items.length; p++ ) {
          switch ( FC.json.items[p].category ) {
            case "BEVERAGE":
              beverages += FC.json.items[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,11964,11965,11966,11970,11968,11971,11972,11792,11975,11976,11977,10026,10027,10030,10037,10039,10001,10011,10018,10019,10020,10036,10029,10035,10010,10016,10017,10022,10012,10013,10014,10004,10005,10006,10007,10038,10280,10002,10003,10009,10021,10028,10044,10138,10023,10024,10025,10031,10032,10033,10034,10040,11930,11701,11708,11931,11768,11702,11703,11704,11707,11933,11743,11963,11706,11751,11743,11705,11930,11743,11777,11713,11715,11716,11780,11717,11932,11718,11719,11933,11772,11702,11980,11934,11720,11721,11722,11749,11760,11782,11724,11743,11725,11726,11727,11770,11768,11935,11772,11729,11780,11746,11937,11730,11939,11940,11731,11772,11942,11733,11967,11941,11768,11717,11731,11706,11738,11770,11782,06390,11901,11780,11768,11702,11763,11739,11740,11944,11743,11946,11743,11749,11760,11788,11780,11757,11954,11741,11742,00501,00544,11743,11743,11746,11747,11750,11749,11760,11751,11751,11752,11947,11754,11706,11743,11755,11961,11779,11779,11948,11757,11743,11743,11950,11949,11726,11950,11951,11952,11763,11747,11750,11775,11805,11953,11764,11954,11955,11766];
    
        if (jQuery.inArray(parseInt(zip), valid_zips) > -1) {
          var hasCoupon = false;
          if(!jQuery.isEmptyObject(FC.json.coupons)) {
            jQuery.each(FC.json.coupons, function(i, coupon){
              if(coupon.name.search(/Neighbor/i) > -1) {
                hasCoupon = true;
              }
            });
          }
    
          if (FC.json.item_count > 0 || hasCoupon) {
            // In valid zips and 1 product or more or a free ship coupon
            FC.customLiveShipping.hide('all');
            FC.customLiveShipping.show(60);
          }
        }
    
        if(!jQuery.isEmptyObject(FC.json.coupons)) {
            jQuery.each(FC.json.coupons, function(i, coupon){
                if(coupon.name.search(/Free T-Shirt/i) > -1) {
                    // Has a coupon that gets free shipping
                    FC.customLiveShipping.hide('all');
                    FC.customLiveShipping.show(60);
                }
            });
        }             
    
    /* END CUSTOM SHIPPING LOGIC */
    
  • Hey adam, unfortunately that still doesn't work. It's displaying the free shipping option along with the other shipping methods regardless of zip code.
  • fc_adamfc_adam FoxyCart Team
    @davidzupec - can you link me to your checkout please?
  • fc_adamfc_adam FoxyCart Team
    @davidzupec,

    Sorry - I had a javascript error in there, change the third line of the code above to this:
    for ( var p = 0; p < FC.json.items.length; p++ ) {
    

    I had a "=" where a "p" should have been.
  • davidzupecdavidzupec Member
    edited October 2014
    Hey Adam, ok now what is happening is the free shipping is no longer showing but those restricted zip codes are unfortunately still getting the shipping options.
  • fc_adamfc_adam FoxyCart Team
    @davdzupec,

    Oh sorry - one more:

    Change this line:
    var zip = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_postal_code").val() : $("#customer_postal_code").val());
    

    To this:
    var zip = FC.json.shipping_address.postal_code;
    
  • Hey Adam, that did it! Thank you so much for all your help. Btw I'm loving the new 2.0 version!
  • brettbrett FoxyCart Team
    Thanks @davidzupec! We spent a ton of time on it, and we're really happy you're liking it :)
  • It definitely shows Brett! My clients love it as well! Keep up the great work.
Sign In or Register to comment.