Skip to content

Instantly share code, notes, and snippets.

@justinstern
Last active December 17, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinstern/5632612 to your computer and use it in GitHub Desktop.
Save justinstern/5632612 to your computer and use it in GitHub Desktop.
This demonstrates how you would setup a new hypothetical weight unit named XX which is equal to 5000 lbs. The new unit is added to the woocommerce admin, and then conversions are provided to the weight standard unit of 'lbs', as well as the conversion from 'XX' to lbs. This approach could be adapted to create new units for the other measurement …
<?php
add_filter( 'woocommerce_catalog_settings', 'add_woocommerce_weight_unit_xx' );
/**
* This adds the new unit to the WooCommerce admin
*/
function add_woocommerce_weight_unit_xx( $settings ) {
foreach ( $settings as &$setting ) {
if ( 'woocommerce_weight_unit' == $setting['id'] ) {
$setting['options']['XX'] = __( 'XX' ); // new unit
}
}
return $settings;
}
add_filter( 'wc_measurement_price_calculator_normalize_table', 'wc_measurement_price_calculator_normalize_table' );
/**
* This converts our new unit to the standard weight unit 'lbs'. Note that if we wanted to support
* metric units a conversion to kg would have to be provided as well.
*/
function wc_measurement_price_calculator_normalize_table( $normalize_table ) {
// xx = 5000 lbs
$normalize_table['XX'] = array( 'factor' => 5000, 'unit' => 'lbs' );
return $normalize_table;
}
add_filter( 'wc_measurement_price_calculator_conversion_table', 'wc_measurement_price_calculator_conversion_table' );
/**
* This converts lbs to our new unit 'XX'. Note that if we wanted to support metric units a conversion
* from kg would have to be provided as well.
*/
function wc_measurement_price_calculator_conversion_table( $conversion_table ) {
// lbs = 1 / 5000 XX
$conversion_table['lbs']['XX'] = array( 'factor' => 5000, 'inverse' => true );
return $conversion_table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment