TotalWeight.php
1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace ACA\WC\Column\Order;
use AC;
use ACP;
use ACP\ConditionalFormat\FormattableConfig;
use WC_Order;
use WC_Order_Item_Product;
class TotalWeight extends AC\Column implements ACP\Export\Exportable, ACP\ConditionalFormat\Formattable {
public function __construct() {
$this->set_type( 'column-order_weight' )
->set_label( __( 'Total Order Weight', 'codepress-admin-columns' ) )
->set_group( 'woocommerce' );
}
public function get_value( $id ) {
$order = wc_get_order( $id );
$weight = $this->get_order_weight( $order );
return sprintf( '%s %s', wc_format_decimal( $weight ), get_option( 'woocommerce_weight_unit' ) );
}
private function get_order_weight( WC_Order $order ): float {
$total_weight = 0;
foreach ( $order->get_items() as $item ) {
if ( ! $item instanceof WC_Order_Item_Product || ! $item->get_product() ) {
continue;
}
$weight = (int) $item->get_quantity() * (float) $item->get_product()->get_weight();
$total_weight += $weight;
}
return $total_weight;
}
public function export() {
return new ACP\Export\Model\Value( $this );
}
public function conditional_format(): ?FormattableConfig {
return new FormattableConfig( ACP\ConditionalFormat\Formatter\SanitizedFormatter::from_ignore_strings( new ACP\ConditionalFormat\Formatter\FloatFormatter() ) );
}
}