PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / WeightCalculator.php

WeightCalculator.php in Packeta 2.0.9, at src/Packetery/Module/WeightCalculator.php

67 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WeightCalculator
4 *
5 * @package Packetery\Module\Weight
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module;
11
12 use Packetery\Module\Options\OptionsProvider;
13 use WC_Order_Item_Product;
14
15 /**
16 * Class WeightCalculator
17 *
18 * @package Packetery\Module\Order
19 */
20 class WeightCalculator {
21
22 /**
23 * Options provider.
24 *
25 * @var OptionsProvider
26 */
27 private $optionsProvider;
28
29 /**
30 * Constructor.
31 *
32 * @param OptionsProvider $optionsProvider Options provider.
33 */
34 public function __construct( OptionsProvider $optionsProvider ) {
35 $this->optionsProvider = $optionsProvider;
36 }
37
38 /**
39 * Calculates order weight ignoring user specified weight.
40 *
41 * @param \WC_Order $order Order.
42 *
43 * @return float
44 */
45 public function calculateOrderWeight( \WC_Order $order ): float {
46 $weight = 0;
47 foreach ( $order->get_items() as $item ) {
48 $quantity = $item->get_quantity();
49 if ( $item instanceof WC_Order_Item_Product ) {
50 $product = $item->get_product();
51
52 if ( is_object( $product ) && method_exists( $product, 'get_weight' ) ) {
53 $productWeight = (float) $product->get_weight();
54 $weight += ( $productWeight * $quantity );
55 }
56 }
57 }
58
59 $weightKg = \wc_get_weight( $weight, 'kg' );
60 if ( is_numeric( $weightKg ) ) {
61 $weightKg += $this->optionsProvider->getPackagingWeight();
62 }
63
64 return $weightKg;
65 }
66 }
67