-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
113 lines (107 loc) · 2.33 KB
/
functions.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
Plugin Name: Related Product Orders
Plugin URI:
description: View related orders on product backend page.
Version: 0.1
Author: Luke Molnar
Author URI:
License: GPL2
*/
function wporg_add_custom_box()
{
$screens = [ 'product' ];
foreach ( $screens as $screen ) {
add_meta_box(
'related-product-orders',
'Related Product Orders',
'rpo_box_html',
$screen
);
}
}
add_action( 'add_meta_boxes', 'wporg_add_custom_box' );
function rpo_box_html( $post )
{
// Get all orders.
$orders = wc_get_orders(
array(
"limit" => -1
)
);
// Get all product IDs.
$product_ids[] = $post->ID;
$children[] = get_children( $post->ID );
foreach ( $children as $child )
{
foreach ( $child as $var )
$product_ids[] = $var->ID;
}
?>
<style>
.rpo-table
{
width: 100%;
border-radius: 5px;
}
.rpo-table td, .rpo-table th
{
border: solid 1px rgb( 220, 220, 220 );
padding: 5px 10px;
border-radius: 5px;
text-align: center;
}
</style>
<table class="rpo-table">
<tr>
<th>Order#</th>
<th>Date / Time</th>
<th>Status</th>
<th>Product / Variation ID</th>
<th>Qty</th>
<th>Subtotal</th>
</tr>
<?php foreach( $orders as $order ): ?>
<?php $items = $order->get_items(); ?>
<?php foreach( $items as $item ): ?>
<?php
$item_id = $item->get_data()["product_id"];
$item_variation_id = $item->get_data()["variation_id"];
$item_qty = $item->get_data()["quantity"];
$item_subtotal = $item->get_data()["subtotal"];
// var_dump( $item->get_data() );
if ( in_array( $item_id, $product_ids ) ): ?>
<tr>
<td>
<a target="_blank" href="/wp-admin/post.php?post=<?php echo $order->get_id(); ?>&action=edit"><?php echo $order->get_id(); ?></a>
</td>
<td>
<?php
$date_created = $order->get_date_created();
if( ! empty( $date_created) )
echo "<time>" . $date_created->date("F j, Y, g:i:s A") . "</time>";
?>
</td>
<td>
<?php echo $order->get_status(); ?>
</td>
<td>
<?php
echo $item_id;
if ( $item_variation_id != 0 )
echo " / " . $item_variation_id;
?>
</td>
<td>
<?php echo $item_qty; ?>
</td>
<td>
<?php echo "$" . $item_subtotal; ?>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
<?php
}