I work on a WordPress website with WooCommerce plugin.
My question:
How to add a new column in User table displaying the number of items. Each customer has items in his/her inventory. I want to count and display the number in the User table in Wordpress.
Here an example on how to display a new column, ‘Billing country’, in Wordpress User table:
This function calculates the total number of items in all completed orders for a specific customer:
function get_customer_items_count( $user_id ) {
$args = array(
'customer_id' => $user_id,
'status' => 'completed', // You can change the status to other order statuses if needed
'limit' => -1 // Retrieve all orders
);
$orders = wc_get_orders( $args );
$items_count = 0;
foreach ( $orders as $order ) {
foreach ( $order->get_items() as $item ) {
$items_count += $item->get_quantity(); // Get the quantity of each item
}
}
return $items_count;
}