Documentation You are here: start » v » 2.0 » shipping » custom_code » trexity

Custom Shipping Code Trexity Integration

The following page details integrating Trexity into your Foxy store for fetching shipping rates, using the Custom Shipping Code functionality available for Foxy stores.

Requirements

  • A Foxy store on version 2.0
  • At least one category configured with a delivery type of “Shipped using live shipping rates”
  • An active Trexity account

Installation

Below is the custom shipping code you'll use to connect to Trexity, and below that is the steps to install it on your store.

// Set to the API key of your Trexity account
const api_key = 'YOUR-TREXITY-API-KEY';
// Set to your origin address for shipments, if left blank, the business address on your Trexity account will be used
const pickup_address = '';
 
 
const shipment = cart['_embedded']['fx:shipment'];
 
let payload = {
    'deliveryAddress': '', // Will be set below
    'orderValue': cart['_embedded']['fx:shipment']['total_item_price'],
    'requirements': {
        // 'legalAgeAlcohol': false,
        // 'perishable': false,
        // 'returnReusables': false
    }
};
if (pickup_address != '') {
    payload.pickupAddress = pickup_address;
}
 
let shipping_address = [];
// Ship to address
if (shipment['address1']) {
    shipping_address.push(shipment['address1']);
}
if (shipment['city']) {
    shipping_address.push(shipment['city']);
}
if (shipment['region']) {
    shipping_address.push(shipment['region']);
}
if (shipment['postal_code']) {
    shipping_address.push(shipment['postal_code']);
}
if (shipment['country']) {
    shipping_address.push(shipment['country']);
}
payload.deliveryAddress = shipping_address.join(", ");
 
try {
    const endpoint = 'https://trexity.app/api/v1_0';
    const response = await fetch(endpoint + '/rates/simple', {
        method: 'POST', 
        body: JSON.stringify(payload),
        headers: {
            "Authorization": "Bearer " + api_key,
            "Content-Type": "application/json"
        }
    });
 
    const data = await response.json();
    if (response.status == 200) {
        for (let r = 0; r < data.data.length; r++) {
            const rate = data.data[r];
 
            if (rate.name) {
                rates.add(10000+r, rate.total/100, '', rate.name);
            }
        }
    } else {
        console.log(data);
    }
} catch(err) {
  console.log(err);
}

Steps

Note, as Trexity requires the full address for getting rates, including the address line 1, it's not possible to get shipping rates from the cart. The steps below includes code to hide the address entry on the cart, and makes it so the checkout doesn't try to fetch shipping if the address line 1 is still empty.

  1. Log in to your store's Foxy administration
  2. Head to the “Configuration” page
  3. Enable the “Add custom header and footer code to your templates” option, and paste the following code into the “custom footer” textarea:
    {% if context == "cart" %}
    <script>
        // We can't get the full address from the cart, so let's just skip it here
        FC.client.event('cart-shipping-options-update').override($.noop);
    </script>
    <style>
        #fc .fc-address-entry {
            display: none;
        }
    </style>
    {% endif %} {# if context == "cart" #}
    {% if context == "checkout" %}
    <script>
    // Overwrite addressHasLocationInfo to check for address1 to require full address for shipping
    var originalAddressHasLocationInfo = FC.util.addressHasLocationInfo;
    FC.util.addressHasLocationInfo = function(address) {
      if (!address.address1) {
        return false;
      } else {
        return originalAddressHasLocationInfo(address);
      }
    }
    </script>
    {% endif %}{# if context == "checkout" #}
  4. Save the configuration section
  5. Head to the “Shipping” page
  6. Scroll down until you see the “Custom Shipping Code” option, and check the option for “use custom code”.
  7. In the code editor that appears, copy and paste the code shown above. Don't save it just yet.
  8. In a new tab, log in to your Trexity account and generate an API key
  9. Copy the API key generated to your clipboard, and in the code you pasted into your Foxy admin above on this line:
    const api_key = 'YOUR-TREXITY-API-KEY';


    Replace the YOUR-TREXITY-API-KEY text with your copied API key

  10. Set the pickup_address string to your specific address, or leave it blank to use the address as configured on your Trexity account
  11. Click the “Update” button to save your new settings
  12. Wait ~10-15 seconds and refresh the page (the custom shipping code feature is a separate system, so takes a moment to initialize, you should see your custom code again after refreshing if it was successful)
  13. You're done! Now you can test it out!

To test out the new rates after saving, you can simply add a product to your cart that belongs to a live rate shippable category. On the cart or checkout, after entering a shipping address, you should see your rates returning from your Trexity account.

Taking It Further

This integration is fairly basic - it just returns all rates from Trexity, and passes it some default values. You can review the Trexity documentation on rate requests with their API here to customise the integration above further as needed.

You can also customise the rates returned, to only output specific rates or work with them further using our custom shipping code API documented here.

Site Tools