php - WooCommerce:SQL 查询 - 如何获取当前的 customer_id?(不是用户 ID)
问题描述
1.问题: WooCommerce 4.1.0:\woocommerce\templates\myaccount dashboard.php
$account_balance
在第 46 行中,我想添加包含一些 SQL 查询的代码,以显示活跃客户在他登录后在“我的帐户”下的帐户(“”)上有多少钱。
我可以获得活动的 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>
我认为我在处理数组或误解查询输出方面犯了一些错误。
(我是 WooCommerce/WordPress 的新手,只对 PHP 和 SQL 有基本的了解。)
非常感谢您在更正我的代码时提供的任何帮助。
解决方案
所以我刚刚编写了一个自定义函数来为您执行此操作,它使用 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();
推荐阅读
- java - 应用程序的 LSOpenURLsWithRole() 失败,错误为 -10810。javaFX 文件
- c# - 在 C# API 中创建枚举 Z3 常量问题
- android - 使用 outputStream android 从蓝牙发送多个文本
- c++ - new 运算符分配的大小比声明的多。为什么
- codenameone - IOS 构建失败,构建提示 codename1.arg.ios.xcode_version=7.3
- android - 在 Android 应用程序中实现类似 Google Maps 的搜索栏
- bash - 如何编写调用子shell的zsh别名?
- javascript - 谷歌说代码无效,我的错误在哪里?
- vba - 通过 VBA 编码写出 1 到 100 之间的素数
- c++ - 重载数组分配运算符