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 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); |
Custom Unit Conversions
↑ Back to top- ft
- sq. ft.
- fl. oz.
- cu. ft.
- lbs
- m
- sq m
- l
- kg
wc_measurement_price_calculator_normalize_table
and accepts/returns an associative array of unit and associated factor to normalize to a standard unit:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Name | Description | Required |
---|---|---|
factor | This multiplied by the custom unit yields the standard unit | Yes |
unit | The 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 above | Yes |
inverse | Optional, indicates that the inverse of factor is used to convert to unit | Yes |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Measurement Conditional Checks
↑ Back to topMeasurements 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); |