Dynamically setting the shipping origin point for live rates
If your store ships from multiple different fulfilment centres, you'll probably want the shipping rates to be calculated correctly based on the correct origin point for the customer. This script allows you to change the shipping origin point dynamically from the one set in the store to your own custom location.
Note: This still only allows for a single origin point for the whole order. This snippet will not allow you to set the origin point differently for only some products in a transaction.
Add the custom javascript
In your store's administration - navigate to the Configuration page, and within the Cart section look for “Add custom header and footer code to your templates”. Enable that option if it isn't already, and place the following code into the “Footer” textbox:
<script> function getCustomOrigin(params) { // Do your custom logic in here if (someCondition) { return {country: "US", region: "CA", postal_code: 90210}; } else { return null; } } FC.client.on("cart-shipping-options-update", function(params) { params.store_address = getCustomOrigin(params); }); FC.client.on("checkout-shipping-options-update", function(params) { params.store_address = getCustomOrigin(params); }); </script>
You'll obviously need to edit the getCustomOrigin()
function to correctly set the dynamic origin points based on your conditions - whether it be the customers address, certain products or categories in the cart etc.
The getCustomOrigin()
function is passed a params
variable, which contains two arrays:
address
: Contains the shipping address for the customer as entered so farfields
: Contains theshipping_city
,shipping_postal_code
,shipping_region
andshipping_region_name
as entered by the customer to trigger this rate request
The getCustomOrigin()
function needs to return an object with three parameters like this: {country: “US”, region: “CA”, postal_code: “90210”}
- with the country
and region
set as the 2 character code for the country and region, and the postal code, all as strings.
If you want to leave the address as the store default, return null
from the getCustomOrigin()
function.