php - How do I sort orders from WooCommerce get_items() function?
one text
I'm building a fundraising site. On the fundraiser pages, I have a table showing all the donations that were made. This utilizes Woo's get_items() function. However, I want to only show 5 orders max and have them sorted by highest donations. I've briefly tried messing with the uasort() function, but I can't seem to get it to work properly. Any advice?
function getTPTopBackerList_function(){
$campaign_id = getTPFundraiserID();
$baker_list = wpcf_function()->get_customers_product($campaign_id);
?>
<table>
<tr>
<th><?php _e('Name', 'wp-crowdfunding'); ?></th>
<th><?php _e('Donate Amount', 'wp-crowdfunding'); ?></th>
</tr>
<?php
foreach($baker_list as $order){
$order = new WC_Order($order);
if ($order->get_status() == 'completed') {
$ordered_items = $order->get_items();
$ordered_this_item = '';
foreach ($ordered_items as $item) {
if (!empty($item['item_meta']['_product_id'][0])) {
if ($campaign_id == $item['item_meta']['_product_id'][0])
$ordered_this_item = $item;
}
}
?>
<tr>
<td>
<?php
if (get_post_meta(get_the_ID(), 'wpneo_mark_contributors_as_anonymous', true) == "1") {
echo __("Anonymous", "wp-crowdfunding");
} else {
$mark_anonymous = get_post_meta($order->get_id(), 'mark_name_anonymous', true);
if ($mark_anonymous === 'true'){
_e('Anonymous', 'wp-crowdfunding');
}else{
echo $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
}
}
?>
</td>
<td>
<?php
echo wpcf_function()->price($order->get_total());
?>
</td>
</tr>
<?php
} //if order completed
}
?>
</table>
<?php
}
Source