3

I'm trying to figure out a function which get current user total number of purchased items (not total sum but items) across as all placed orders. So far I have found this (which doesn't work) - but again this function should get total sum and not items. Been trying to edit it to work but no success so far.

public function get_customer_total_order() {
$customer_orders = get_posts( array(
    'numberposts' => - 1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => array( 'shop_order' ),
    'post_status' => array( 'wc-completed' )
) );

$total = 0;
foreach ( $customer_orders as $customer_order ) {
    $order = wc_get_order( $customer_order );
    $total += $order->get_total();
}

return $total;
}

Any ideas?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Viktor
  • 71
  • 6

3 Answers3

4

Updated (Taking in account the item quantity)

The following very lightweight function will get the total purchased items count by a customer:

function get_user_total_purchased_items( $user_id = 0 ){
    global $wpdb;

    $customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;

    return (int) $wpdb->get_var( "
        SELECT SUM(woim.meta_value)
        FROM {$wpdb->prefix}woocommerce_order_items AS woi
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
        INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
        WHERE woi.order_item_type = 'line_item'
        AND p.post_type LIKE 'shop_order'
        AND p.post_status IN ('wc-completed')
        AND pm.meta_key LIKE '_customer_user'
        AND pm.meta_value LIKE '$customer_id'
        AND woim.meta_key LIKE '_qty'
    " );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


USAGE Example

1) Display the current user total purchased items count:

<?php echo '<p>Total purchased items: ' . get_user_total_purchased_items() . '</p>'; ?>

2) Display the total purchased items count for a given user ID:

// Here the user ID is 105
<?php echo '<p>Total purchased items: ' . get_user_total_purchased_items(105) . '</p>'; ?>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! Tried it but it only seems to count each product once. So if a customer orders 2x product1 and 4x product2, the function gets a total count of 2 items rather than 6. Doesn't seem to take quantity within each order into consideration maybe? @LoicTheAztec – Viktor Aug 01 '18 at 07:30
  • @Viktor I have updated my answer… Now, It will give the correct value based on order items quantity sum. – LoicTheAztec Aug 01 '18 at 07:53
  • Works perfectly! Thanks a lot! Only one thing, is it possible to return 0 if null/ if no previous completed orders? – Viktor Aug 01 '18 at 08:29
  • 1
    @Viktor Just updated… Now it should display 0 if customer has no orders yet. – LoicTheAztec Aug 01 '18 at 08:36
  • 1
    It does! Thanks a million! Amazing – Viktor Aug 01 '18 at 08:45
0
        function get_user_total_purchased_items_by_email( $email ){
            global $wpdb;
            if (empty($email)) {
                return;
            }

            return (int) $wpdb->get_var( "
                SELECT SUM(woim.meta_value)
                FROM {$wpdb->prefix}woocommerce_order_items AS woi
                INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
                INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
                INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
                WHERE woi.order_item_type = 'line_item'
                AND p.post_type LIKE 'shop_order'
                AND p.post_status IN ('wc-completed')
                AND pm.meta_key LIKE '_customer_user'
                AND pm.meta_value LIKE '$email'
                AND woim.meta_key LIKE '_qty'
            " );
        }

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Usage Example

<?php echo '<p>Total purchased items: ' . get_user_total_purchased_items_by_email('youremail@mail.com') . '</p>'; ?>
-1

Try this below code

global $wpdb;
$customer_orders = get_posts( array(
    'numberposts' => - 1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => array( 'shop_order' ),
    'post_status' => array( 'wc-completed' )
) );
$customer_orders = json_decode(json_encode($customer_orders),true);


$total_product_count = 0;
foreach ( $customer_orders as $customer_order_data ) {



        $Order_ID=$customer_order_data['ID'];
        $Select_Order_Details = $wpdb->get_results( "SELECT  COUNT(order_item_id) AS total_product_count FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_type='line_item' and order_id = $Order_ID");
        $Select_Order_Details = json_decode(json_encode($Select_Order_Details),true);

        foreach($Select_Order_Details as $Product_Count)
        {
                $total_product_count=$total_product_count+$Product_Count['total_product_count'];
        }

}
echo ($total_product_count); exit;
VinothRaja
  • 1,405
  • 10
  • 21