Skip to main content
Services · Revision #4

What is the volume service?

What is the volume service?

The volume service is used to normalize volumes into the store's base unit (cm^3 by default) as well as convert between different units.

Units

By default the volume service has the following units:

  • mm^3
  • cm^3
  • m^3

Adding a Unit

Use addUnit() to add a unit, for example we can add millilitres & litres:

\Aero\Common\Facades\Volume::addUnit('ml', 'ml');

\Aero\Common\Facades\Volume::addUnit('l', 'L')
    ->setNormalizer(function ($value) {
        return $value * 1000;
    });

setNormalizer defines how to get from that unit to the base unit, which is cm^3.

Removing a Unit

\Aero\Common\Facades\Volume::removeUnit('m^3');

Converting between Units

You can use convert() to convert between any two units, or normalize() to convert into the store's base unit.

\Aero\Common\Facades\Volume::convert(1000, 'mm^3', 'cm^3');

\Aero\Common\Facades\Volume::normalize('mm^3', 1000);

Setting a base Unit

\Aero\Common\Facades\Volume::setBaseUnit('ml');

When you change base unit, database values will still remain in cm^3. But volumes will be displayed in the new unit on screens such as the fulfillment create page.