WooCommerce Measurement Price Calculator Developer Documentation

The developer documentation is provided as a courtesy to developers in order to help out with customizations.

Please note that while we provide these samples as a reference, customizations are not included as part of our support policy – we are unable to modify them or implement them on your site. If you’re not sure how to add custom code to your site or you want to modify these snippets, we recommend hiring a Woo Expert to assist.

Adding Custom Units

↑ Back to top
Custom units can be added by taking advantage of one of the core WooCommerce filters: woocommerce_products_general_settings. This filter is passed an array of the WooCommerce general settings (including measurement settings) and returns the same. This allows us to inject custom weight, dimension, area or volume units. Note that it’s not strictly important that a custom unit be of the same type to which it is added (weight, dimension, area or volume). For instance, WooCommerce includes the dimension units include ‘ft’, ‘in’, ‘cm’, ‘m’, etc, but a custom unit such as ‘day’ or ‘word’ to represent an amount of days for a lesson product, or a number of words for a proofreading service priced by word count, respectively, could be added to the dimension unit set and considered as “lengths” when configuring a calculator product. To add a unit, iterate over the settings array and look for the setting with id of ‘woocommerce_dimension_unit’, ‘woocommerce_weight_unit’, ‘woocommerce_area_unit’ or ‘woocommerce_volume_unit’ depending on what you need. As an example we’ll add the unit “league” to the ‘woocommerce_dimension_unit’ set:
/**
* This adds the new unit to the WooCommerce admin
*/
function add_woocommerce_dimension_unit_league( $settings ) {
foreach ( $settings as &$setting ) {
if ( 'woocommerce_dimension_unit' == $setting['id'] ) {
$setting['options']['league'] = __( 'league' ); // new unit
}
}
return $settings;
}
add_filter( 'woocommerce_products_general_settings', 'add_woocommerce_dimension_unit_league' );
This will add the unit to the core WooCommerce Catalog settings page, as well as to the Measurement Price Calculator product settings, allowing the custom unit to be used like any other. This is all that’s required to add a basic unit, however adding a new unit that can be converted to and from other units requires some additional custom code, as explained below.

Custom Unit Conversions

↑ Back to top
The above simple code works great for independent units like ‘day’ or ‘word’, but to add a custom unit that’s related to and can be converted from/to other units of the same type requires the use of two additional filters, to provide a conversion to and from one of the Measurement Price Calculator’s “standard” units. As an example, say you wish to add the dimension “league” and support conversions of “league” to miles, feet, millimeters, or any other dimensional unit. The first step is to calculate the conversion of your custom unit to one of the Measurement Price Calculator “standard” units, which include the following for English and SI:

  • ft
  • sq. ft.
  • fl. oz.
  • cu. ft.
  • lbs
  • m
  • sq m
  • l
  • kg
Because “league” is not a metric unit we’ll use a conversion to feet. According to google 1 league = 18,228.3465 feet. The first filter to add is named wc_measurement_price_calculator_normalize_table and accepts/returns an associative array of unit and associated factor to normalize to a standard unit:
<?php
add_filter( 'wc_measurement_price_calculator_normalize_table', 'wc_measurement_price_calculator_normalize_table' );
/**
* Add conversions for any custom units to the appropriate standard unit
*/
function wc_measurement_price_calculator_normalize_table( $normalize_table ) {
// 1 league = 18,228.3465 ft
$normalize_table['league'] = array( 'factor' => 18228.3465, 'unit' => 'ft' );
return $normalize_table;
}
The normalization array for a custom unit includes two required and one optional element:
NameDescriptionRequired
factorThis multiplied by the custom unit yields the standard unitYes
unitThe standard unit the custom unit normalizes to. For accuracy it’s best to stick with one system of measurements. For instance if you were adding the custom unit “decaliter” you would use “l” for the standard unit, rather than “fl. oz.” See the list of accepted standard units aboveYes
inverseOptional, indicates that the inverse of factor is used to convert to unitYes
Finally the conversion from the relevant standard units back to the custom unit must be provided via the wc_measurement_price_calculator_conversion_table filter. This filter accepts/returns an associative array of standard unit to all other units to which they can be converted in a similar format to the normalize filter above. In our example of adding the dimensional unit “league” we want to allow leagues to be converted to all other dimension unit systems (English and SI), so we add the following:
<?php
add_filter( 'wc_measurement_price_calculator_conversion_table', 'wc_measurement_price_calculator_conversion_table' );
/**
* This converts standard units to all other compatible units
*/
function wc_measurement_price_calculator_conversion_table( $conversion_table ) {
// 1 ft = 1/18228.3465 leagues
$conversion_table['ft']['league'] = array( 'factor' => 18228.3465, 'inverse' => true );
// 1 m = 0.000179985601 leagues
$conversion_table['m']['league'] = array( 'factor' => 0.000179985601 );
return $conversion_table;
}
The conversion array includes a factor element, which when multiplied by the standard unit (ie ft or m) yields the custom unit (ie league). The optional ‘inverse’ element can be set to true for conversions that require division.

Measurement Conditional Checks

↑ Back to top

Measurements Enabled

You can easily check whether or not measurements are enabled for a product by using the calculator_enabled() method in the WC_Price_Calculator_Product static class. You should pass in the product to check if the measurement calculator is enabled for that product.
global $product;
$measurement = WC_Price_Calculator_Product::calculator_enabled( $product );
This will let you execute your code depending on whether the calculator is enabled or not (regardless of which calculator mode is used). For example, here’s a snippet that would add a heading above the calculator to the product page, but only if it’s enabled for the product:
/**
* Example: checking if the calculator is being used for a product
*
* Adds a heading above the Measurement Price Calculator if enabled
*/
function sv_mpc_heading() {
if ( ! class_exists( 'WC_Price_Calculator_Product' ) ) {
return;
}
global $product;
$measurement = WC_Price_Calculator_Product::calculator_enabled( $product );
if ( $measurement ) {
echo '<h4>Test Heading</h4>';
}
}
add_filter( 'woocommerce_before_add_to_cart_button', 'sv_mpc_heading', 4 );

User Documentation

↑ Back to top
Return to user documentation →
Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.