Skip to content

Instantly share code, notes, and snippets.

@aayla-secura
Last active June 6, 2023 21:33
Show Gist options
  • Save aayla-secura/40ea9c2da127914774743987ae209bc7 to your computer and use it in GitHub Desktop.
Save aayla-secura/40ea9c2da127914774743987ae209bc7 to your computer and use it in GitHub Desktop.
GetPaid Wordpress plugin: Code snippet to list users who've purchased an item (or any of its children)
<?php
add_action( 'add_meta_boxes', 'add_getpaid_item_invoices_meta_box', 50 );
function add_getpaid_item_invoices_meta_box() {
add_meta_box( 'wpinv_item_invoices', __( 'Item Purchases', 'invoicing' ), 'show_getpaid_item_purchases', 'wpi_item', 'normal' );
}
function show_getpaid_item_purchases( $post ) {
// get this post and all children
$posts = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'wpi_item'
) );
$posts[] = $post;
$post_ids = array_map( function( $post ) {
return $post->ID;
}, $posts );
print_invoice_details_table_start();
// search through all invoices and find those that contain this item
$max_pages = -1;
for ($page = 1; $max_pages < 0 || $page <= $max_pages; $page++) {
$invoices = wpinv_get_invoices( array(
'limit' => false,
'paginate' => true,
'page' => $page,
) );
$max_pages = $invoices->max_num_pages;
foreach ( $invoices->invoices as $inv ) {
foreach ( $inv->get_items() as $n => $inv_item ) {
if ( in_array( $inv_item->get_id(), $post_ids ) ) {
print_invoice_details_table_row( $inv, $n, $inv_item );
}
}
}
}
print_invoice_details_table_end();
}
function print_invoice_details_table_row( $inv, $n, $item ) {
$user_id = $inv->get_user_id();
$user_info = get_userdata( $user_id );
$user_url = esc_url( add_query_arg( 'user_id', $user_id, admin_url( 'user-edit.php' ) ) );
?>
<tr class="item item-<?php echo ( $n % 2 == 0 ? 'even' : 'odd' ); ?>">
<td class="title">
<a href="<?php echo $user_url; ?>" target="_blank"><?php echo $user_info->display_name; ?></a>
</td>
<td class="title">
<a href="<?php echo get_edit_post_link( $item->get_id() ); ?>" target="_blank"><?php echo esc_html( $item->get_name() ); ?></a>
</td>
<td class="qty">
<?php
echo $item->get_quantity();
?>
</td>
<td class="title">
<a href="<?php echo get_permalink( $inv->get_id() ); ?>" target="_blank"><?php echo esc_html( $inv->get_number() ); ?></a>
</td>
<!-- <td class="total">
<?php
echo wpinv_the_price( $inv->get_total(), $inv->get_currency() );
?>
</td> -->
</tr>
<?php
}
function print_invoice_details_table_start() {
?>
<div class="wpinv-items-wrap" id="wpinv_items_wrap">
<table id=" wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th class="title">Customer</th>
<th class="title">Ticket</th>
<th class="qty">Quantity</th>
<th class="title">Invoice</th>
<!-- <th class="total">Invoice Price</th> -->
</tr>
</thead>
<tbody class="wpinv-line-items">
<?php
}
function print_invoice_details_table_end() {
?>
</tbody>
</table>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment