phpsqlwoocommerceuserid

WooCommerce: SQL query - how to get current customer_id ? (NOT user_id)


1. The problem: WooCommerce 4.1.0: \woocommerce\templates\myaccount dashboard.php

in Line 46 I would like to add my code containing some SQL queries to show the active customer how much money he has on his account ("$account_balance") under "my account" after he has logged in.

I can get the active user_id:

$user_id = get_current_user_id();
$user = new WC_Customer( $user_id );

But I do not know how to get the current customer_id (required because the tables do not contain user_id).

2. What I’ve tried:

I created a table in phpMyAdmin called transfers containing the following columns:

transaction_id
customer_id
transfer_date
eur_transferred
eur_refunded
first_name
last_name
order_id

From this custom table I want to get all inbound and outbound transactions (eur_transferred and eur_refunded).

The other table I am using is wc_order_stats from which I want to get net_total. However, wc_order_stats contains the column customer_id but not user_id (the assigned numbers differ in the database).

The 3 SQL queries should return the sums of

3. My code:

<?php

//
// variable declarations
//

global $account_balance, $transferred_total, $refunded_total, $orders_total, $net_total;
global $wpdb;

$account_balance = 0.00;
$transferred_total = 0.00;
$refunded_total = 0.00;
$orders_total = 0.00;

$user_id = get_current_user_id();
$user = new WC_Customer( $user_id );

$customer_id = get_customer_id();
$customer = new WC_Customer( $customer_id );

//
// first output testing
//

echo "Customer id: " . print_r($customer_id). "<br>"; // I always get NULL, also tried with sprintf and echo
echo "Customer id: " . $customer_id . "<br>";
print_r($customer_id);
sprintf($customer_id);

print_r ($customer); // I always get NULL, also tried with sprintf and echo
echo "Customer: " . $customer. "<br><br>";
echo "Customer: " . print_r($customer);

echo "User id: " . $user_id . "<br>"; // user_id output works
var_dump($user["id"]["display_name"]["role"]);
echo var_dump($user['id']);

//
// Three SQL queries
//

$transferred_total = $wpdb->get_var("

    SELECT SUM(eur_transferred) FROM {$wc_prefix}transfers WHERE customer_id = $customer_id
");

echo "<b>" . "Transfers grand total: " . "</b>" . $transferred_total . " €<br>";

$refunded_total = $wpdb->get_var("

    SELECT SUM(eur_refunded) FROM {$wc_prefix}transfers WHERE customer_id = $customer_id
");

echo "<b>" . "Refunds grand total: " . '</b>' . $refunded_total . " €<br>";

$orders_total = $wpdb->get_var("

    SELECT SUM(net_total) FROM {$wc_prefix}wc_order_stats WHERE customer_id = $customer_id
");

echo "<b>Orders grand total: </b>" . $orders_total . " €<br><br>";
var_dump ($orders_total); // solely for testing output

$account_balance = $transferred_total + $refunded_total - $orders_total

var_dump ($account_balance);
echo $account_balance;
echo "<h1>Your current account balance: </h1><b>" . $account_balance . "</b>";

?>
</p>

I think I made some mistakes concerning handling of arrays or misinterpreted outputs of queries.


Solution

  • So I just wrote a custom function to do this for you that uses Woo's customer_lookup table. This table has both the customer_id and the user_id.

    // place in your theme functions.php or a functionality plugin
    function my_get_customer_id_from_user_id($user_id = -1)
    {
        // if no use_id passed as a param use the current user id
        if (-1 === $user_id) {
            $user_id = get_current_user_id();
        }
    
        global $wpdb;
    
        // use woo's customer lookup table which has both customer_id and user_id
        $tablename = $wpdb->prefix . "wc_customer_lookup";
    
        // setup query
        $sql = $wpdb->prepare("SELECT customer_id FROM {$tablename} WHERE user_id = %d", $user_id);
    
    
        $customer_id = $wpdb->get_var($sql);
    
    
        return $customer_id;
    }
    
    // usage in your template
    $customer_id = my_get_customer_id_from_user_id();