The following page details integrating Trexity into your Foxy store for fetching shipping rates, using the Custom Shipping Code functionality available for Foxy stores.
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); }
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.
{% 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" #}
const api_key = 'YOUR-TREXITY-API-KEY';
Replace the YOUR-TREXITY-API-KEY
text with your copied API key
pickup_address
string to your specific address, or leave it blank to use the address as configured on your Trexity accountTo 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.
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.