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:custom_live_rate_endpoint [2014/09/22 07:59] adamv:2.0:snippets:custom_live_rate_endpoint [2017/04/26 07:02] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Custom Live Rate Endpoint ====== ====== Custom Live Rate Endpoint ======
 +
 +<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.
 +</WRAP>
  
 If you have requirements outside of the live shipping rate integrations that FoxyCart currently has, whether to make use of a specific shipping carrier, or make use of functionality that we don't current support, you can use the following snippet to roll your own endpoint. If you have requirements outside of the live shipping rate integrations that FoxyCart currently has, whether to make use of a specific shipping carrier, or make use of functionality that we don't current support, you can use the following snippet to roll your own endpoint.
Line 21: Line 25:
 Your endpoint should print the result out in the following format for them to be handled correctly within your store: Your endpoint should print the result out in the following format for them to be handled correctly within your store:
  
-=== Success ===+=== Success JSON ===
 <code javascript>{ <code javascript>{
-    "ok":true, +  "ok":true, 
-    "data":+  "data":
-        "shipping_results":+    "shipping_results":
-            +      
-                "service_id": 57, +        "service_id": 57, 
-                "price": 642.2, +        "price": 642.2, 
-                "method":"FedEx", +        "method":"FedEx", 
-                "service_name": "International Economy" +        "service_name": "International Economy" 
-            }, +      }, 
-            +      
-                "service_id": 56, +        "service_id": 56, 
-                "price": 765.47, +        "price": 765.47, 
-                "method": "FedEx", +        "method": "FedEx", 
-                "service_name": "International Priority" +        "service_name": "International Priority" 
-            +      
-        +    
-    }+  }
 }</code> }</code>
  
-=== Errors ===+<WRAP center round info 90%> 
 +**Note:** The ''service_id'' value needs be set to an integer. 
 +</WRAP> 
 + 
 + 
 +=== Errors JSON ===
  
 <code javascript>{ <code javascript>{
Line 49: Line 58:
 }</code> }</code>
  
 +=== Example JSONP Output ===
 +<code php>
 +<?php
 +// Mitigate against Rosetta Flash vulnerability: https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
 +header('Content-Type:application/json;charset=utf-8');
 +header('X-Content-Type-Options: nosniff');
 +$output = '/**/';
 +
 +// Create some JSON. Preferably using your programming languages native JSON functionality, not like this :)
 +$rates = '{
 +  "ok":true,
 +  "data": {
 +    "shipping_results": [
 +      {
 +        "service_id": 57,
 +        "price": 642.2,
 +        "method":"FedEx",
 +        "service_name": "International Economy"
 +      },
 +      {
 +        "service_id": 56,
 +        "price": 765.47,
 +        "method": "FedEx",
 +        "service_name": "International Priority"
 +      }
 +    ]
 +  }
 +}';
 +
 +// Output JSONP. Wrap the JSON in the callback, prepended with the comment
 +echo $output . $_GET['callback'] . '(' . $rates . ')';
 +</code>
  
 ==== Step 2: Add javascript ==== ==== Step 2: Add javascript ====
  
-Add the following in the "footer" section of the template configuration'"inject custom codeoption in your store's FoxyCart administration, or right at the end of the ''cart include'' template (not the ''cart'' or ''checkout'' templates, the ''cart include'' template will include it there for you).+Add the following in the “custom footer” field of the [[https://admin.foxycart.com/admin.php?ThisAction=TemplateConfig|template configuration]]'“Add custom header and footer code to your templates” option of your store's FoxyCart administration.
  
 <code javascript>{% if context == 'cart' or context == 'checkout' %} <code javascript>{% if context == 'cart' or context == 'checkout' %}
 <script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
-    FC.api.getShippingOptions(address) {+    FC.api.getShippingOptions = function(address) {
         var request_data = {         var request_data = {
             'shipping_address_name': address.address_name,             'shipping_address_name': address.address_name,
Line 80: Line 121:
             });             });
         }).promise();         }).promise();
-    }+    };
 </script> </script>
 {% endif %}</code> {% endif %}</code>
  
-In the above code, you'll need to update ''YOUR-ENDPOINT-URL'' to be your actual endpoint script.+ 
 +<WRAP center round info 90%> 
 +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> 
 + 
 +In the above code, you'll need to update ''YOUR-ENDPOINT-URL'' to be your actual endpoint script. <wrap important>You have to wrap the url of your actual endpoint script in single quotes (' ').</wrap>

Site Tools