This is an old revision of the document!


Custom Live Rate 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.

Requirements

This script requires communicating with an external script. As the cart and checkout pages where this call will happen are secured over HTTPS, to allow for the communication to happen, your endpoint needs to be secured over HTTPS as well. Without it, the requests will be blocked by the browser and customers won't see any rates.

Setup

Step 1: Your Endpoint

On your side, your endpoint will be passed a group of post values containing the customers shipping address.

  • shipping_address_name Used for multiship - will contain the multiship name
  • shipping_state 2 character code if auto-completed, or the name as entered
  • shipping_country 2 character country code like US or GB
  • shipping_city:
  • shipping_postal_code

Your endpoint should print the result out in the following format for them to be handled correctly within your store:

Success

{
	"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"
			}
		]
	}
}

Errors

{
	"ok":false,
	"details": "Error Message"
}

Step 2: Add javascript

Add the following in the “footer” section of the template configuration's “inject custom code” option 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).

{% if context == 'cart' or context == 'checkout' %}
<script type="text/javascript" charset="utf-8">
	FC.api.getShippingOptions(address) {
		var request_data = {
	        'shipping_address_name': address.address_name,
	        'shipping_state': address.region,
	        'shipping_country': address.country,
	        'shipping_city': address.city,
	        'shipping_postal_code': address.postal_code
	    };
 
		return $.Deferred(function (deferred) {
	        $.ajax({
	            url: YOUR-ENDPOINT-URL,
	            dataType: 'jsonp',
	            data: request_data
	        }).done(function (response) {
	            if (response.ok) {
	                deferred.resolve(response);
	            } else {
	                deferred.reject(Error(response.details || 'Unknown error'));
	            }
	        }).fail(function (jqXHR, textStatus, errorThrown) {
	            deferred.reject(Error(errorThrown));
	        });
	    }).promise();
	}
</script>
{% endif %}

In the above code, you'll need to update YOUR-ENDPOINT-URL to be your actual endpoint script.

Site Tools