/** * Gets the volume of the product, if one is defined, in woocommerce product units * * @since 3.0 * @param WC_Product $product the product * @return WC_Price_Calculator_Measurement total volume measurement for the product, or null */ public static function get_volume_measurement($product) { $measurement = null; // if a length and width are defined, use that. We allow large and small dimensions // (mm, km, mi) which don't make much sense to use as volumes, but // we have no choice but to support them to some extent, so convert // them to something more reasonable if ($product->length && $product->width && $product->height) { $volume = $product->length * $product->width * $product->height; switch (get_option('woocommerce_dimension_unit')) { case 'mm': $volume *= 0.001; // convert to ml $unit = 'ml'; break; case 'km': $volume *= 1000000000; // convert to cu m $unit = 'cu m'; break; case 'mi': $volume *= 5451776000; // convert to cu yd $unit = 'cu. yd.'; break; } $unit = WC_Price_Calculator_Measurement::to_volume_unit(get_option('woocommerce_dimension_unit')); $measurement = new WC_Price_Calculator_Measurement($unit, $volume, 'volume', __('Volume', WC_Measurement_Price_Calculator::TEXT_DOMAIN)); // convert to the product volume units $measurement->set_unit(get_option('woocommerce_volume_unit')); } // if there's an area and height, next use that $area = WC_Price_Calculator_Product::get_product_meta($product, 'area'); if ($area && $product->height) { $area_unit = get_option('woocommerce_area_unit'); $area_measurement = new WC_Price_Calculator_Measurement($area_unit, $area); $dimension_unit = get_option('woocommerce_dimension_unit'); $dimension_measurement = new WC_Price_Calculator_Measurement($dimension_unit, $product->height); // determine the volume, in common units $dimension_measurement->set_common_unit($area_measurement->get_unit_common()); $volume = $area_measurement->get_value_common() * $dimension_measurement->get_value_common(); $volume_unit = WC_Price_Calculator_Measurement::to_volume_unit($area_measurement->get_unit_common()); $measurement = new WC_Price_Calculator_Measurement($volume_unit, $volume, 'volume', __('Volume', WC_Measurement_Price_Calculator::TEXT_DOMAIN)); // and convert to final volume units $measurement->set_unit(get_option('woocommerce_volume_unit')); } // finally if they overrode the length/width/height with a volume value, use that $volume = WC_Price_Calculator_Product::get_product_meta($product, 'volume'); if ($volume) { $measurement = new WC_Price_Calculator_Measurement(get_option('woocommerce_volume_unit'), $volume, 'volume', __('Volume', WC_Measurement_Price_Calculator::TEXT_DOMAIN)); } // if no measurement, just create a default empty one if (!$measurement) { $measurement = new WC_Price_Calculator_Measurement(get_option('woocommerce_volume_unit'), 0, 'volume', __('Volume', WC_Measurement_Price_Calculator::TEXT_DOMAIN)); } return $measurement; }