/** * 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; }
/** * Gets the measurements settings for the current calculator. If a frontend * label is not set for a measurement, the unit will be used. If the * returned measurements include more than one, for instance length, width or * area, height, a common unit will be available on all of them to faciliate * deriving a compound measurement (ie area or volume) * * @return array of WC_Price_Calculator_Measurement objects */ public function get_calculator_measurements() { $calculator_type = $this->get_calculator_type(); $measurements = array(); // special case for the dimension calculator, pluck out the enabled measurement (one of length, width or height) and return by itself if ('dimension' == $calculator_type) { foreach ($this->settings[$this->get_calculator_type()] as $name => $value) { if ('pricing' != $name && 'yes' == $value['enabled']) { $measurements[] = new WC_Price_Calculator_Measurement($value['unit'], 1, $name, $value['label'], $value['editable'], $this->get_options($name)); } } } else { // otherwise just return the measurement settings with a default value (excluding the 'pricing' setting) $measurements = array(); $common_unit = null; foreach ($this->settings[$this->get_calculator_type()] as $name => $value) { if ('pricing' != $name) { $measurement = new WC_Price_Calculator_Measurement($value['unit'], 1, $name, $value['label'], $value['editable'], $this->get_options($name)); // generate a common unit to use for this set of measurements based on the first measurement encountered // then set that common unit on the subsequent. This allows us to have the (admittedly crazy) case of // a Volume (AxH) calculator with area in acres and height in meters, so the common unit will be // sq. ft. and ft. respectively. That way we can multiply A * H and get an answer in known units (cu. ft.) // regardless of the mixture of units that the constituent measurements use if (is_null($common_unit)) { $common_unit = $measurement->get_unit_common(); } else { $measurement->set_common_unit($common_unit); } $measurements[] = $measurement; } } } return $measurements; }