PluginProbe
Packeta / 1.6.3
Packeta v1.6.3
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 1.6.3, at src/Packetery/Module/WeightCalculator.php

65 lines 1.2 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
11 namespace Packetery\Module;
12
13 use Packetery\Module\Options;
14
15 /**
16 * Class WeightCalculator
17 *
18 * @package Packetery\Module\Order
19 */
20 class WeightCalculator {
21
22 /**
23 * Options provider.
24 *
25 * @var Options\Provider
26 */
27 private $optionsProvider;
28
29 /**
30 * Constructor.
31 *
32 * @param Options\Provider $optionsProvider Options provider.
33 */
34 public function __construct( Options\Provider $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 $product = $item->get_product();
50
51 if ( is_object( $product ) && method_exists( $product, 'get_weight' ) ) {
52 $productWeight = (float) $product->get_weight();
53 $weight += ( $productWeight * $quantity );
54 }
55 }
56
57 $weightKg = \wc_get_weight( $weight, 'kg' );
58 if ( $weightKg ) {
59 $weightKg += $this->optionsProvider->getPackagingWeight();
60 }
61
62 return $weightKg;
63 }
64 }
65