getShippingRates( )
Retrieves shipping rates provided by a custom shipping rates extension.
Description
This function is automatically called by Wix eCommerce to retrieve the shipping rates provided by your extension.
This happens when actions are performed on the cart/checkout entities/pages and/or when there is a change to any property in the options
parameter. For example, when an item is added to the cart.
Notes:
- Every time
getShippingRates()
is called, the response is cached. This cache is valid for 10 minutes and is used until a change is made to any property in theoptions
parameter, at which point the cache is refreshed.- If your extension fails to respond within 10 seconds, the call will purposefully fail to ensure a smooth user experience. We recommend optimizing your logic: calls to external APIs take longer and are outside our control; use fewer wixData actions when possible.
Where to find getShippingRates()
When you add the Shipping Rates custom extension,
a folder is automatically added to your site. Use the <my-extension-name>.js
file in the folder to write your custom shipping rates code.
For more information on customizing your shipping rate options, see Tutorial: Shipping Rates Custom Extension.
Syntax
function getShippingRates(options: Options): Promise<ShippingRates>
getShippingRates Parameters
NAME
TYPE
DESCRIPTION
Shipping origin, shipping destination, and general configurations.
Returns
Fulfilled - Available shipping rates.
Return Type:
NAME
TYPE
DESCRIPTION
Available shipping rates. These define the shipping rate options that are displayed to site visitors on the Cart and Checkout pages.
Was this helpful?
shippingRates
return value1export const getShippingRates = (options) => {23 return {4 "shippingRates": [{5 "code": "usps-international",6 "title": "USPS - International",7 "logistics": {8 "deliveryTime": "2-5 days"9 },10 "cost": {11 "price": "15",12 "currency": "USD",13 "additionalCharges": [{14 "price": "10",15 "type": "HANDLING_FEE",16 "details": "Handling fee of $5 applied for fragile items."17 }]18 }19 }]20 };21};
1export const getShippingRates = (options) => {2 return getShippingRatesByQuantity(options)3}45// Tally the quantity of all items6function calculateTotalCartQuantity(lineItems) {7 return lineItems.reduce((acc, lineItem) => acc += lineItem.quantity, 0);8}910// Return shipping rates based on the calculated items' quantity11function getShippingRatesByQuantity(options) {12 const cartQuantity = calculateTotalCartQuantity(options.lineItems);1314 if (cartQuantity >= 3) {15 return {16 shippingRates: [{17 code: 'quantity_more_3',18 title: 'Quantity > 3',19 logistics: {20 deliveryTime: '3-5 Days'21 },22 cost: {23 price: '10.00',24 // Currency value is taken from provided options object25 currency: "USD"26 }27 }]28 };29 };3031 // If cartQuantity is less than 3, the following shipping rate is returned32 return {33 shippingRates: [{34 code: 'quantity_more_1',35 title: '1 Item',36 logistics: {37 deliveryTime: '3-5 Days'38 },39 cost: {40 price: '20.00',41 currency: "USD"42 }43 }]44 };45};