order = $order; } /** * Returns order weight in KG. * * @param float $default_weight . * * @return float - precision 3 */ public function get_order_weight_kg( $default_weight = 0.0 ) { if ( function_exists( 'fs_calculate_order_weight' ) ) { $weight = fs_calculate_order_weight( $this->order ); } else { $weight = 0.0; if ( count( $this->order->get_items() ) > 0 ) { foreach ( $this->order->get_items() as $item ) { $weight += $this->get_raw_item_weight( $item ); } } } if ( (float) $weight === 0.0 ) { return $this->round_weight( $default_weight ); } return $this->convert_wc_weight_to_kg( $weight ); } /** * @param WC_Order_Item $item Order Item. * * @return float */ private function get_raw_item_weight( WC_Order_Item $item ) { if ( $item['product_id'] > 0 ) { $product = $item->get_product(); if ( $product instanceof WC_Product && ! $product->is_virtual() ) { return $product->get_weight() * $item['qty']; } } return 0; } /** * Convert WooCommerce weight to KG * * @param string $wc_weight Weight in WooCommerce default unit weight. * * @return float weight in kg - precision 3. */ private function convert_wc_weight_to_kg( $wc_weight ) { $weight = wc_get_weight( $wc_weight, 'kg' ); $weight = str_replace( ',', '.', $weight ); return $this->round_weight( (float) $weight ); } /** * @param $weight * * @return float */ private function round_weight( $weight ) { $weight = round( $weight, 3 ); return $weight; } }