Skip to content

vtex/lean-shipping-calculator

Repository files navigation

lean-shipping-calculator

Utilities library to for VTEX lean shipping options

Documentation

Install

$ npm install @vtex/lean-shipping-calculator -S

API

getLeanShippingOptions({ logisticsInfo, activeChannel, isScheduledDeliveryActive })

Returns lean shipping options (cheapest, fastest and combined)

logisticsInfo

Type: Array

Logistics info for each item

activeChannel

Type: String
Default: delivery

Active channel

isScheduledDeliveryActive

Type: boolean
Default: false


getOptionsDetails(delivery)

Get more details about each lean option

delivery

Type: Object

Lean shipping options


getSelectedDeliveryOption

Get lean shipping option that is current selected

delivery

Type: Object

Object with lean shipping options


Usage

getLeanShippingOptions

Return all options are only returned if they are worthy according to the calculation.

const leanShippingCalculator = require('@vtex/lean-shipping-calculator')

const options = leanShippingCalculator.getLeanShippingOptions({
  logisticsInfo,
  activeChannel,
})

console.log(options)
// {
//   cheapest: [{item1}, {item2}],
//   fastest:  [{item1}, {item2}],
//   combined: [{item1}, {item2}]
// }

An example of the function returning only cheapest option:

console.log(options)
// {
//   cheapest: [{item1}, {item2}]
// }

getOptionsDetails

Returns more details about each lean shipping option

const leanShippingCalculator = require('@vtex/lean-shipping-calculator')

const optionDetails = leanShippingCalculator.getOptionsDetails({
  [CHEAPEST]: cheapestOption,
  [COMBINED]: combinedOption,
  [FASTEST]: fastestOption,
})

console.log(optionDetails)
// [
//   {
//     averageEstimatePerItem: 520200,
//     id: 'CHEAPEST',
//     packagesLength: 2,
//     price: 1000,
//     shippingEstimate: '8bd',
//   },
//   {
//     averageEstimatePerItem: 220200,
//     id: 'COMBINED',
//     packagesLength: 2,
//     price: 3000,
//     shippingEstimate: '2bd',
//   },
//   {
//     averageEstimatePerItem: 300200,
//     id: 'FASTEST',
//     packagesLength: 2,
//     price: 1500,
//     shippingEstimate: '5bd',
//   }
// ]

getSelectedDeliveryOption

Returns the selected lean options

const leanShippingCalculator = require('@vtex/lean-shipping-calculator')

const optionToBeSelected = leanShippingCalculator.getSelectedDeliveryOption({
  optionsDetails = null,
  newCombined,
  newFastest,
  newCheapest,
  activeDeliveryOption,
})

console.log(optionToBeSelected) // CHEAPEST