Broadleaf Microservices
  • v1.0.0-latest-prod

Weight Calculation Service

Overview

The WeightCalculationService can sum weights of different units. This is used during checkout to get the total weight of a fulfillment group. Out of the box, Pounds and Kilograms are supported. Other units can easily be added via properties.

The WeightCalculationService utilizes the Indriya library for calculating and converting units of measure.

Configuration

MeasurementProperties exposes a few properties for configuring the WeightCalculationService.

unitAliases

broadleaf.shipping.service.measurements.unitAliases

Maps weight units from the Indriya library classes to String labels. For instance, the following configuration maps "Pound", "POUND", and "POUNDS" to systems.uom.common.USCustomary.POUND. This configuration should contain all weight units that could be used on the site. One particular place to check is UnitsWeightTypeOptionEnum from CatalogServices metadata. Values from that enum will be used for the weight units on cart and fulfillment items.

broadleaf:
  shipping:
    service:
      measurements:
        unitAliases:
          systems.uom.common.USCustomary.POUND:
            - Pound
            - POUND
            - POUNDS
          tech.units.indriya.unit.Units.KILOGRAM:
            - Kilogram
            - KILOGRAM
            - KILOGRAMS

Unit Aliases are processed during app startup. The method #addUnitLabels will INFO log configured aliases.

weightPrecision

broadleaf.shipping.service.measurements.weightPrecision

The number of significant digits to use for weight calculations. This applies to all digits of a number, before and after the decimal. This is important when units are converted during weight calculations.

The default value of 5 should be enough precision for most applications. Too high of a precision can introduce floating point arithmetic artifacts when converting and summing weights.

Usage

The DefaultWeightCalculationService exposes methods to compare weights and to get the total weight of a package. Total package weight can be calculated from either a FulfillmentPricingRequest for carts during checkout or an OrderFulfillment when creating shipping labels for submitted orders.

The weight conversions are handled by the Indriya library and the details are beyond the scope of this document.

The pieces come together in the #sumWeights method, where each item’s com.broadleafcommerce.shipping.domain.Weight is converted to a tech.units.indriya.ComparableQuantity and summed with tech.units.indriya.ComparableQuantity#add.