![](/img/trans.png)
[英]MySQL query to get the primary key(customer_id) from a table and to compare it with the current session id(customer_id)
[英]WooCommerce: SQL query - how to get current customer_id ? (NOT user_id)
1.問題: WooCommerce 4.1.0:\woocommerce\templates\myaccount dashboard.php
在第 46 行中,我想添加我的代碼,其中包含一些 SQL 查詢,以顯示活躍客戶登錄后在“我的賬戶”下的賬戶(“ $account_balance
”)中有多少錢。
我可以獲得活動的 user_id:
$user_id = get_current_user_id();
$user = new WC_Customer( $user_id );
但我不知道如何獲取當前的customer_id
(需要,因為表不包含user_id
)。
2.我嘗試過的:
我在phpMyAdmin中創建了一個名為L16_transfers
的表,其中包含以下列:
transaction_id
customer_id
transfer_date
eur_transferred
eur_refunded
first_name
last_name
order_id
從這個自定義表中,我想獲取所有入站和出站交易( eur_transferred
和eur_refunded
)。
我正在使用的另一個表是L16_wc_order_stats
我想從中獲取net_total
。 但是, L16_wc_order_stats
包含列customer_id
但不包含user_id
(分配的數字在數據庫中不同)。
3 SQL 查詢應返回總和
3.我的代碼:
<?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 L16_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 L16_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 L16_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>
我認為我在處理 arrays 或誤解的查詢輸出方面犯了一些錯誤。
所以我剛剛編寫了一個自定義 function 來為您使用 Woo 的customer_lookup
表。 該表同時具有customer_id
和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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.