| @@ -6,12 +6,15 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | - | |
| 11 | 10 | namespace Packetery\Module; |
| 12 | 11 | |
| 13 | -use Packetery\Module\Options; | |
| 12 | +use Packetery\Core\CoreHelper; | |
| 13 | +use Packetery\Module\Framework\WcAdapter; | |
| 14 | +use Packetery\Module\Options\OptionsProvider; | |
| 15 | +use WC_Order; | |
| 16 | +use WC_Order_Item_Product; | |
| 14 | 17 | |
| 15 | 18 | /** |
| 16 | 19 | * Class WeightCalculator |
| 17 | 20 | * |
| @@ -19,46 +22,48 @@ | ||
| 19 | 22 | */ |
| 20 | 23 | class WeightCalculator { |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | - * Options provider. | |
| 24 | - * | |
| 25 | - * @var Options\Provider | |
| 26 | + * @var OptionsProvider | |
| 26 | 27 | */ |
| 27 | 28 | private $optionsProvider; |
| 28 | 29 | |
| 29 | 30 | /** |
| 30 | - * Constructor. | |
| 31 | - * | |
| 32 | - * @param Options\Provider $optionsProvider Options provider. | |
| 31 | + * @var WcAdapter | |
| 33 | 32 | */ |
| 34 | - public function __construct( Options\Provider $optionsProvider ) { | |
| 33 | + private $wcAdapter; | |
| 34 | + | |
| 35 | + public function __construct( | |
| 36 | + OptionsProvider $optionsProvider, | |
| 37 | + WcAdapter $wcAdapter | |
| 38 | + ) { | |
| 35 | 39 | $this->optionsProvider = $optionsProvider; |
| 40 | + $this->wcAdapter = $wcAdapter; | |
| 36 | 41 | } |
| 37 | 42 | |
| 38 | 43 | /** |
| 39 | 44 | * Calculates order weight ignoring user specified weight. |
| 40 | 45 | * |
| 41 | - * @param \WC_Order $order Order. | |
| 46 | + * @param WC_Order $order Order. | |
| 42 | 47 | * |
| 43 | 48 | * @return float |
| 44 | 49 | */ |
| 45 | - public function calculateOrderWeight( \WC_Order $order ): float { | |
| 46 | - $weight = 0; | |
| 50 | + public function calculateOrderWeight( WC_Order $order ): float { | |
| 51 | + $weight = 0.0; | |
| 47 | 52 | foreach ( $order->get_items() as $item ) { |
| 48 | 53 | $quantity = $item->get_quantity(); |
| 49 | - $product = $item->get_product(); | |
| 54 | + if ( $item instanceof WC_Order_Item_Product ) { | |
| 55 | + $product = $item->get_product(); | |
| 50 | 56 | |
| 51 | - if ( is_object( $product ) && method_exists( $product, 'get_weight' ) ) { | |
| 52 | - $productWeight = (float) $product->get_weight(); | |
| 53 | - $weight += ( $productWeight * $quantity ); | |
| 57 | + if ( is_object( $product ) && method_exists( $product, 'get_weight' ) ) { | |
| 58 | + $productWeight = (float) $product->get_weight(); | |
| 59 | + $weight += ( $productWeight * $quantity ); | |
| 60 | + } | |
| 54 | 61 | } |
| 55 | 62 | } |
| 56 | 63 | |
| 57 | - $weightKg = \wc_get_weight( $weight, 'kg' ); | |
| 58 | - if ( $weightKg ) { | |
| 59 | - $weightKg += $this->optionsProvider->getPackagingWeight(); | |
| 60 | - } | |
| 64 | + $weightKg = $this->wcAdapter->getWeight( $weight, 'kg' ); | |
| 65 | + $weightKg += $this->optionsProvider->getPackagingWeight(); | |
| 61 | 66 | |
| 62 | - return $weightKg; | |
| 67 | + return CoreHelper::simplifyWeight( $weightKg ); | |
| 63 | 68 | } |
| 64 | 69 | } |