Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v:2.0:snippets:flat_rate_shipping_modification [2017/08/28 07:48] – [Changelog] adamv:2.0:snippets:flat_rate_shipping_modification [2018/09/17 13:18] (current) – [Flat Rate Shipping Modification] adam
Line 2: Line 2:
  
 <WRAP center round important 90%> <WRAP center round important 90%>
-**This snippet has been replaced.** In place of the following snippet, we now have [[v:2.0:shipping#custom_shipping_endpoint|a custom shipping endpoint]] feature in FoxyCart 2.0 that allows you to provide custom rates to your customers without needing to add custom javascript to change the way the checkout works. Please use the custom shipping endpoint instead of this snippet. This page will remain for reference+**This snippet has been replaced.** In place of the following snippet, we now have [[v:2.0:shipping:custom_code|a custom shipping code]] feature in FoxyCart 2.0 that allows you to modify returned live rates and provide custom rates to your customers without needing to add extra javascript to change the way the checkout works. **Please use [[v:2.0:shipping:custom_code|the new custom shipping code functionality]] instead of this snippet.** [[v:2.0:shipping:custom_code:migrating|Review this page for notes]] on migrating to the new functionality. This page will remain for reference.</WRAP>
- +
-If you're using a hosted solution like Wix, Webflow, Squarespace, etc. and don't have access to web hosting you can add custom scripts to, building your own custom endpoint may not an option. Feel free to continue using this method in that instance.</WRAP>+
  
 <WRAP center round info 80%> <WRAP center round info 80%>
Line 21: Line 19:
 Update all categories to 'Shipped using a flat rate fee' with a value of 0 in the 'Product Delivery Option' section. Update all categories to 'Shipped using a flat rate fee' with a value of 0 in the 'Product Delivery Option' section.
  
-===== Step 2: Add Javascript =====+ 
 +===== Step 2: Disable Shipping Rate Signing ===== 
 + 
 +To ensure the snippet works as normal, you'll also need to ensure that shipping rate signing is disabled for your store. To do that, head to the "shipping" section of your store's FoxyCart administration, and at the bottom of the page ensure that "enable shipping rate signing" is disabled. 
 +===== Step 3: Add Javascript =====
  
 Add the following in the "footer" section of the template configuration's “Add custom header and footer code to your templates” option. You will find this option within the "Cart" group on the "Configuration" section of your store's FoxyCart administration. Add the following in the "footer" section of the template configuration's “Add custom header and footer code to your templates” option. You will find this option within the "Cart" group on the "Configuration" section of your store's FoxyCart administration.
Line 43: Line 45:
     };     };
          
-    /* Flat Rate Shipping Modification Logic v2.0.*/+    /* Flat Rate Shipping Modification Logic v2.0.12 */
     FC.customFlatRates.add=function(a,b,c,d){FC.customFlatRates.shipping_results.push({method:c,price:b,service_id:a,service_name:d})};FC.customFlatRates.hide=function(a){a=FC.customFlatRates.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.customFlatRates.shipping_results[a[b]].hide=!0};FC.customFlatRates.show=function(a){a=FC.customFlatRates.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.customFlatRates.shipping_results[a[b]].hide=!1};     FC.customFlatRates.add=function(a,b,c,d){FC.customFlatRates.shipping_results.push({method:c,price:b,service_id:a,service_name:d})};FC.customFlatRates.hide=function(a){a=FC.customFlatRates.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.customFlatRates.shipping_results[a[b]].hide=!0};FC.customFlatRates.show=function(a){a=FC.customFlatRates.filterShippingOptions(a);for(var b=0;b<a.length;b++)FC.customFlatRates.shipping_results[a[b]].hide=!1};
     FC.customFlatRates.remove=function(a){a=FC.customFlatRates.filterShippingOptions(a);a=a.sort(function(a,b){return b-a});for(var b=a.length-1;0<=b;b--)FC.customFlatRates.shipping_results.splice(a[b],1)};     FC.customFlatRates.remove=function(a){a=FC.customFlatRates.filterShippingOptions(a);a=a.sort(function(a,b){return b-a});for(var b=a.length-1;0<=b;b--)FC.customFlatRates.shipping_results.splice(a[b],1)};
     FC.customFlatRates.update=function(a,b,c,d){a=FC.customFlatRates.filterShippingOptions(a);for(var e=0;e<a.length;e++){if("number"===typeof b||"string"===typeof b&&""!==b){var f=FC.customFlatRates.modifyPrice(FC.customFlatRates.shipping_results[a[e]].price,b);FC.customFlatRates.shipping_results[a[e]].price=f}"string"===typeof c&&(FC.customFlatRates.shipping_results[a[e]].method=c);"string"===typeof d&&(FC.customFlatRates.shipping_results[a[e]].service_name=d)}};     FC.customFlatRates.update=function(a,b,c,d){a=FC.customFlatRates.filterShippingOptions(a);for(var e=0;e<a.length;e++){if("number"===typeof b||"string"===typeof b&&""!==b){var f=FC.customFlatRates.modifyPrice(FC.customFlatRates.shipping_results[a[e]].price,b);FC.customFlatRates.shipping_results[a[e]].price=f}"string"===typeof c&&(FC.customFlatRates.shipping_results[a[e]].method=c);"string"===typeof d&&(FC.customFlatRates.shipping_results[a[e]].service_name=d)}};
     FC.customFlatRates.reset=function(){FC.customFlatRates.shipping_results=[];FC.customFlatRates.error_message=!1};FC.customFlatRates.error=function(a){FC.customFlatRates.error_message=a};     FC.customFlatRates.reset=function(){FC.customFlatRates.shipping_results=[];FC.customFlatRates.error_message=!1};FC.customFlatRates.error=function(a){FC.customFlatRates.error_message=a};
-    FC.api.getShippingOptions=function(a){var b={address_name:a.address_name,state:a.region,country:a.country,city:a.city,postal_code:a.postal_code};FC.customFlatRates.reset();FC.customFlatRates.logic(b);for(var b=!1,c=FC.customFlatRates.shipping_results.length-1;0<=c;c--)FC.customFlatRates.shipping_results[c].hide?FC.customFlatRates.shipping_results.splice(c,1):FC.customFlatRates.shipping_results[c].service_id==a.shipping_service_id&&(b=!0);b||(a.shipping_service_description="",a.shipping_service_id= +    FC.api.getShippingOptions=function(a){var b={address_name:a.address_name,state:a.region,country:a.country,city:a.city,postal_code:a.postal_code};FC.customFlatRates.reset();FC.customFlatRates.logic(b);b=!1;for(var c=FC.customFlatRates.shipping_results.length-1;0<=c;c--)FC.customFlatRates.shipping_results[c].hide?FC.customFlatRates.shipping_results.splice(c,1):FC.customFlatRates.shipping_results[c].service_id==a.shipping_service_id&&(b=!0);b||(a.shipping_service_description="",a.shipping_service_id= 
-    0,a.total_shipping=0,a.total_future_shipping=0);if(0<FC.customFlatRates.length&&(FC.customFlatRates.shipping_results.sort(function(a,b){return a.price-b.price}),FC.customFlatRates.config.autoSelect&&0==a.shipping_service_id||1==FC.customFlatRates.shipping_results.length)){if(FC.json.has_multiship)for(c=0;c<FC.json.multiship_data.length;c++)FC.json.multiship_data[c].address_name==a.address_name&&(FC.json.multiship_data[c].shipping_service_id=FC.customFlatRates.shipping_results[0].service_id,FC.json.multiship_data[c].shipping_service_description= +    0,a.total_shipping=0,a.total_future_shipping=0);if(0<FC.customFlatRates.shipping_results.length&&(FC.customFlatRates.shipping_results.sort(function(a,b){return a.price-b.price}),FC.customFlatRates.config.autoSelect&&0==a.shipping_service_id||1==FC.customFlatRates.shipping_results.length)){if(FC.json.has_multiship)for(c=0;c<FC.json.multiship_data.length;c++)FC.json.multiship_data[c].address_name==a.address_name&&(FC.json.multiship_data[c].shipping_service_id=FC.customFlatRates.shipping_results[0].service_id, 
-    FC.customFlatRates.shipping_results[0].method+" "+FC.customFlatRates.shipping_results[0].service_name);else FC.json.shipping_address.shipping_service_id=FC.customFlatRates.shipping_results[0].service_id,FC.json.shipping_address.shipping_service_description=FC.customFlatRates.shipping_results[0].method+" "+FC.customFlatRates.shipping_results[0].service_name;FC.json.show_shipping_tbd=!1}return $.Deferred(function(a){setTimeout(function(){FC.customFlatRates.error_message?a.reject(Error(FC.customFlatRates.error_message)): +    FC.json.multiship_data[c].shipping_service_description=FC.customFlatRates.shipping_results[0].method+" "+FC.customFlatRates.shipping_results[0].service_name);else FC.json.shipping_address.shipping_service_id=FC.customFlatRates.shipping_results[0].service_id,FC.json.shipping_address.shipping_service_description=FC.customFlatRates.shipping_results[0].method+" "+FC.customFlatRates.shipping_results[0].service_name;FC.json.show_shipping_tbd=!1}return $.Deferred(function(a){setTimeout(function(){FC.customFlatRates.error_message? 
-    a.resolve({ok:!0,data:{shipping_results:FC.customFlatRates.shipping_results}})},10)})}; +    a.reject(Error(FC.customFlatRates.error_message)):a.resolve({ok:!0,data:{shipping_results:FC.customFlatRates.shipping_results}})},1E3)})}; 
-    FC.customFlatRates.filterShippingOptions=function(a){if("number"==typeof a)for(var b=0;b<FC.customFlatRates.shipping_results.length;b++){if(FC.customFlatRates.shipping_results[b].service_id==a)return[b]}else{if("string"==typeof a){for(var c=[],d={},b=0;b<FC.customFlatRates.shipping_results.length;b++){var e=FC.customFlatRates.shipping_results[b];d[b]=e.method+" "+e.service_name}if("all"!=a.toLowerCase()){a=/(fedex|usps|ups)?\s?([\w\s]+)?/i.exec(a);if(void 0==a||""==a)return;for(b in d)void 0!=a[1]&&+    FC.customFlatRates.filterShippingOptions=function(a){if("number"==typeof a)for(var b=0;b<FC.customFlatRates.shipping_results.length;b++){if(FC.customFlatRates.shipping_results[b].service_id==a)return[b]}else{if("string"==typeof a){var c=[],d={};for(b=0;b<FC.customFlatRates.shipping_results.length;b++){var e=FC.customFlatRates.shipping_results[b];d[b]=e.method+" "+e.service_name}if("all"!=a.toLowerCase()){a=/(fedex|usps|ups)?\s?([\w\s]+)?/i.exec(a);if(void 0==a||""==a)return;for(b in d)void 0!=a[1]&&
     ""!=a[1]&&-1==d[b].toLowerCase().indexOf(a[1].toLowerCase())?delete d[b]:void 0!=a[2]&&""!=a[2]&&-1==d[b].toLowerCase().indexOf(a[2].toLowerCase())&&delete d[b]}for(b in d)c.push(parseInt(b));return c}if("object"==typeof a){c=[];for(b=0;b<a.length;b++)for(d=0;d<FC.customFlatRates.shipping_results.length;d++)FC.customFlatRates.shipping_results[d].service_id==a[b]&&c.push(a[b]);return c}}};     ""!=a[1]&&-1==d[b].toLowerCase().indexOf(a[1].toLowerCase())?delete d[b]:void 0!=a[2]&&""!=a[2]&&-1==d[b].toLowerCase().indexOf(a[2].toLowerCase())&&delete d[b]}for(b in d)c.push(parseInt(b));return c}if("object"==typeof a){c=[];for(b=0;b<a.length;b++)for(d=0;d<FC.customFlatRates.shipping_results.length;d++)FC.customFlatRates.shipping_results[d].service_id==a[b]&&c.push(a[b]);return c}}};
-    FC.customFlatRates.modifyPrice=function(a,b){b=b.toString();var c=/([\+\-\=\*\/])?(\d+(?:\.\d+)?)(\%)?/.exec(b);a=parseFloat(a);var d=parseFloat(c[2]);void 0!=c[3]&&""!=c[3]&&(d=d/100*a);switch(void 0==c[1]&&""!=c[1]?"=":c[1]){case "+":a+=d;break;case "-":a-=d;break;case "/":a/=d;break;case "*":a*=d;break;default:a=d}return 0>a?0:a};+    FC.customFlatRates.modifyPrice=function(a,b){b=b.toString();var c=/([\+\-=\*\/])?(\d+(?:\.\d+)?)(%)?/.exec(b);a=parseFloat(a);var d=parseFloat(c[2]);void 0!=c[3]&&""!=c[3]&&(d=d/100*a);switch(void 0==c[1]&&""!=c[1]?"=":c[1]){case "+":a+=d;break;case "-":a-=d;break;case "/":a/=d;break;case "*":a*=d;break;default:a=d}return 0>a?0:a};
     function requireShipping(){var a=!1;FC.customFlatRates.config.onLocationChange=!1;if(FC.json.has_multiship)for(i=0;i<FC.json.multiship_data.length;i++)FC.json.multiship_data[i].has_shippable_products&&!FC.json.multiship_data[i].has_live_rate_shippable_products&&(FC.json.multiship_data[i].has_live_rate_shippable_products=!0,FC.json.shipping_address.custom_flat_rates_applied=!0,0==FC.json.multiship_data[i].shipping_service_id&&(FC.json.show_address_entry=!0,"cart"==FC.json.context&&(FC.json.show_shipping_tbd=     function requireShipping(){var a=!1;FC.customFlatRates.config.onLocationChange=!1;if(FC.json.has_multiship)for(i=0;i<FC.json.multiship_data.length;i++)FC.json.multiship_data[i].has_shippable_products&&!FC.json.multiship_data[i].has_live_rate_shippable_products&&(FC.json.multiship_data[i].has_live_rate_shippable_products=!0,FC.json.shipping_address.custom_flat_rates_applied=!0,0==FC.json.multiship_data[i].shipping_service_id&&(FC.json.show_address_entry=!0,"cart"==FC.json.context&&(FC.json.show_shipping_tbd=
-    !0)));FC.json.shipping_address.has_shippable_products&&!FC.json.shipping_address.has_live_rate_shippable_products&&(FC.json.shipping_address.has_live_rate_shippable_products=!0,FC.json.has_current_live_rate_shipping=!0,FC.json.has_current_flat_rate_shipping=!1,a=!0,0==FC.json.shipping_address.shipping_service_id&&(FC.json.show_address_entry=!0,FC.json.show_shipping_tbd=!0));a&&(FC.Template(FC.json.context).clearOutput(),FC[FC.json.context].render());$.each(FC.json.has_multiship?FC.json.multiship_data: +    !0)));FC.json.shipping_address.has_shippable_products&&!FC.json.shipping_address.has_live_rate_shippable_products&&(FC.json.shipping_address.has_live_rate_shippable_products=!0,FC.json.has_current_live_rate_shipping=!0,FC.json.has_current_flat_rate_shipping=!1,a=!0,0==FC.json.shipping_address.shipping_service_id&&(FC.json.show_address_entry=!0,FC.json.show_shipping_tbd=!0));a&&(a=!1,1==$('[data-fc-id="block-coupon-entry"] .fc-alert p').length&&(a=$('[data-fc-id="block-coupon-entry"] .fc-alert p').html(), 
-    [FC.json.shipping_address],function(a,c){(!FC.customFlatRates.config.onLocationChange&&FC.util.addressHasLocationInfo(c)||FC.customFlatRates.config.onLocationChange)&&c.has_live_rate_shippable_products&&FC[FC.json.context].getShippingOptions({address:c})})}function saveShippingId(a,b,c){FC.customFlatRates.shipping_service_ids=[];$.each(FC.json.has_multiship?FC.json.multiship_data:[FC.json.shipping_address],function(a,b){FC.customFlatRates.shipping_service_ids[a]=b.shipping_service_id})}+    FC.util.addError("coupon",a),a=!0),FC.Template(FC.json.context).clearOutput(),FC[FC.json.context].render(),a&&FC.util.removeError("coupon"));$.each(FC.json.has_multiship?FC.json.multiship_data:[FC.json.shipping_address],function(a,c){(!FC.customFlatRates.config.onLocationChange&&FC.util.addressHasLocationInfo(c)||FC.customFlatRates.config.onLocationChange)&&c.has_live_rate_shippable_products&&FC[FC.json.context].getShippingOptions({address:c})})} 
 +    function saveShippingId(a,b,c){FC.customFlatRates.shipping_service_ids=[];$.each(FC.json.has_multiship?FC.json.multiship_data:[FC.json.shipping_address],function(a,b){FC.customFlatRates.shipping_service_ids[a]=b.shipping_service_id})}
     function restoreShippingId(a,b,c){a=/ThisAction=(\w+)/;if(void 0!==c&&c.hasOwnProperty("url")){if(-1!=c.url.search(a))var d=c.url.match(a)[1];else-1<c.url.indexOf("/cart")&&(d="cart");-1==$.inArray(d,["SaveCartContactInfo","GetAddressByPostalCode","SaveShippingServiceInfo","GetTaxes"])&&(FC.customFlatRates.hasOwnProperty("shipping_service_ids")&&$.each(FC.json.has_multiship?FC.json.multiship_data:[FC.json.shipping_address],function(a,b){0==b.shipping_service_id&&(b.shipping_service_id=void 0===FC.customFlatRates.shipping_service_ids[a]?     function restoreShippingId(a,b,c){a=/ThisAction=(\w+)/;if(void 0!==c&&c.hasOwnProperty("url")){if(-1!=c.url.search(a))var d=c.url.match(a)[1];else-1<c.url.indexOf("/cart")&&(d="cart");-1==$.inArray(d,["SaveCartContactInfo","GetAddressByPostalCode","SaveShippingServiceInfo","GetTaxes"])&&(FC.customFlatRates.hasOwnProperty("shipping_service_ids")&&$.each(FC.json.has_multiship?FC.json.multiship_data:[FC.json.shipping_address],function(a,b){0==b.shipping_service_id&&(b.shipping_service_id=void 0===FC.customFlatRates.shipping_service_ids[a]?
     0:FC.customFlatRates.shipping_service_ids[a])}),requireShipping())}FC.customFlatRates.shipping_service_ids=[]}$(document).ajaxStart(saveShippingId);$(document).ajaxComplete(restoreShippingId);     0:FC.customFlatRates.shipping_service_ids[a])}),requireShipping())}FC.customFlatRates.shipping_service_ids=[]}$(document).ajaxStart(saveShippingId);$(document).ajaxComplete(restoreShippingId);
Line 69: Line 72:
 If you have created your own cart or checkout templates, ensure you've followed the steps to include the custom code placeholders as [[https://wiki.foxycart.com/v/2.0/templates/intermediate-automagicache#include_custom_code_placeholders|detailed here]] If you have created your own cart or checkout templates, ensure you've followed the steps to include the custom code placeholders as [[https://wiki.foxycart.com/v/2.0/templates/intermediate-automagicache#include_custom_code_placeholders|detailed here]]
 </WRAP> </WRAP>
-===== Step 3: Update Configuration =====+===== Step 4: Update Configuration =====
  
-This script currently provides a single option to alter the way the script works. The following code is found at the top of the snippet you just copied in step 2, and if you'd like to alter the setting detailed below, you can alter that where you've just pasted the snippet code.+This script currently provides a single option to alter the way the script works. The following code is found at the top of the snippet you just copied in step 3, and if you'd like to alter the setting detailed below, you can alter that where you've just pasted the snippet code.
  
 <code javascript>FC.customFlatRates.config = { <code javascript>FC.customFlatRates.config = {
Line 79: Line 82:
   * ''autoSelect'': If set to true, the top option for rates will be automatically selected after your custom logic has run.   * ''autoSelect'': If set to true, the top option for rates will be automatically selected after your custom logic has run.
  
-===== Step 4: Customise Shipping Options =====+===== Step 5: Customise Shipping Options =====
  
 Now the fun part, based on whatever criteria you want, add in the different shipping options you require for your site. Add your custom code between the ''/* BEGIN CUSTOM SHIPPING LOGIC */'' and ''/* END CUSTOM SHIPPING LOGIC */'' lines in the first script block. Now the fun part, based on whatever criteria you want, add in the different shipping options you require for your site. Add your custom code between the ''/* BEGIN CUSTOM SHIPPING LOGIC */'' and ''/* END CUSTOM SHIPPING LOGIC */'' lines in the first script block.
Line 208: Line 211:
  
 **Example:**  **Example:** 
-  * ''FC.customFlatRates.error('Sorry, we can't ship to Canada');''+  * ''FC.customFlatRates.error('Sorry, we can\'t ship to Canada');''
  
 ---- ----
Line 361: Line 364:
   * 2015/10/15 - v2.0.8 - Update to the events that the snippet uses to hook into the page   * 2015/10/15 - v2.0.8 - Update to the events that the snippet uses to hook into the page
   * 2017/08/28 - v2.0.9 - Correcting logic to only try sorting and selecting a default rate if there are actual results   * 2017/08/28 - v2.0.9 - Correcting logic to only try sorting and selecting a default rate if there are actual results
 +  * 2017/09/13 - v2.0.10 - Small bugfix from previous version relating to auto-selecting rates
 +  * 2017/11/20 - v2.0.11 - Small change to the delay until the custom rates are returned to the page
 +  * 2018/05/03 - v2.0.12 - Persist coupon error message temporarily to allow error response to be seen

Site Tools