title: WooCommerce: SQL query - how to get current customer_id ? (NOT user_id)
body: <p><strong>1. The problem:</strong>
  WooCommerce 4.1.0:
  \woocommerce\templates\myaccount
  dashboard.php</p>
  <p>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 (&quot;<code>$account_balance</code>&quot;) under &quot;my account&quot; after he has logged in.</p>
  <p>I can get the active user_id:</p>
  <pre><code>$user_id = get_current_user_id();
  $user = new WC_Customer( $user_id );
  </code></pre>
  <p>But I do not know how to get the current <code>customer_id</code> (required because the tables do not contain <code>user_id</code>).</p>
  <p><strong>2. What I’ve tried:</strong></p>
  <p>I created a table in <em><strong>phpMyAdmin</strong></em> called <code>L16_transfers</code> containing the following columns:</p>
  <pre><code>transaction_id
  customer_id
  transfer_date
  eur_transferred
  eur_refunded
  first_name
  last_name
  order_id
  </code></pre>
  <p>From this custom table I want to get all inbound and outbound transactions (<code>eur_transferred</code> and
  <code>eur_refunded</code>).</p>
  <p>The other table I am using is <code>L16_wc_order_stats</code> from which I want to get <code>net_total</code>.
  However, <code>L16_wc_order_stats</code> contains the column <code>customer_id</code> but not <code>user_id</code> (the assigned numbers differ in the database).</p>
  <p>The 3 SQL queries should return the sums of</p>
  <ul>
  <li>transfers</li>
  <li>refunds</li>
  <li>orders
  which I will add / subtract to get the total current balance.</li>
  </ul>
  <p><strong>3. My code:</strong></p>
  <pre><code>&lt;?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 &quot;Customer id: &quot; . print_r($customer_id). &quot;&lt;br&gt;&quot;; // I always get NULL, also tried with sprintf and echo
  echo &quot;Customer id: &quot; . $customer_id . &quot;&lt;br&gt;&quot;;
  print_r($customer_id);
  sprintf($customer_id);
  
  print_r ($customer); // I always get NULL, also tried with sprintf and echo
  echo &quot;Customer: &quot; . $customer. &quot;&lt;br&gt;&lt;br&gt;&quot;;
  echo &quot;Customer: &quot; . print_r($customer);
  
  echo &quot;User id: &quot; . $user_id . &quot;&lt;br&gt;&quot;; // user_id output works
  var_dump($user[&quot;id&quot;][&quot;display_name&quot;][&quot;role&quot;]);
  echo var_dump($user['id']);
  
  //
  // Three SQL queries
  //
  
  $transferred_total = $wpdb-&gt;get_var(&quot;
  
      SELECT SUM(eur_transferred) FROM L16_transfers WHERE customer_id = $customer_id
  &quot;);
  
  echo &quot;&lt;b&gt;&quot; . &quot;Transfers grand total: &quot; . &quot;&lt;/b&gt;&quot; . $transferred_total . &quot; €&lt;br&gt;&quot;;
  
  $refunded_total = $wpdb-&gt;get_var(&quot;
  
      SELECT SUM(eur_refunded) FROM L16_transfers WHERE customer_id = $customer_id
  &quot;);
  
  echo &quot;&lt;b&gt;&quot; . &quot;Refunds grand total: &quot; . '&lt;/b&gt;' . $refunded_total . &quot; €&lt;br&gt;&quot;;
  
  $orders_total = $wpdb-&gt;get_var(&quot;
  
      SELECT SUM(net_total) FROM L16_wc_order_stats WHERE customer_id = $customer_id
  &quot;);
  
  echo &quot;&lt;b&gt;Orders grand total: &lt;/b&gt;&quot; . $orders_total . &quot; €&lt;br&gt;&lt;br&gt;&quot;;
  var_dump ($orders_total); // solely for testing output
  
  $account_balance = $transferred_total + $refunded_total - $orders_total
  
  var_dump ($account_balance);
  echo $account_balance;
  echo &quot;&lt;h1&gt;Your current account balance: &lt;/h1&gt;&lt;b&gt;&quot; . $account_balance . &quot;&lt;/b&gt;&quot;;
  
  ?&gt;
  &lt;/p&gt;
  </code></pre>
  <p>I think I made some mistakes concerning handling of arrays or misinterpreted outputs of queries.</p>