如何获取当前用户的所有订单(针对Orders.php页面)
我正在尝试构建几个短代码来显示一些信息,例如用户下的所有订单的名称和订单号。
这些是短代码。第一个返回 0 作为值,我不明白我错在哪里。第二个不显示任何值。任何人都可以帮助我指出错误吗?
我需要使用短代码,因为我正在重新创建 order.php 页面
// Get All Orders numbers of current user
add_shortcode( 'prcsed_order_numb' , 'prcsed_order_1' );
function prcsed_order_1(){
$customer = new WC_Customer( get_current_user_id() );
$order = new WC_Order( $customer_order );
if ( is_a( $order, 'WC_Order' ) ) {
return $order->get_order_number();
}
}
// Get All Orders Name of current user
add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
function prcsed_order_2(){
// Get an instance of the WC_Order object
$order = new WC_Order( $customer_order );
foreach ( $customer_orders->orders as $customer_order ) {
return $order = $item->get_name();
}
}
I'm trying to build several shortcodes that display some information like name and order number of all orders placed by a user.
These are the shortcodes. The first one returns 0 as a value, I don't understand where I'm wrong. The second displays no value. Can anyone help me by pointing out the errors?
I need to work with shortcodes because I am recreating the orders.php page
// Get All Orders numbers of current user
add_shortcode( 'prcsed_order_numb' , 'prcsed_order_1' );
function prcsed_order_1(){
$customer = new WC_Customer( get_current_user_id() );
$order = new WC_Order( $customer_order );
if ( is_a( $order, 'WC_Order' ) ) {
return $order->get_order_number();
}
}
// Get All Orders Name of current user
add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
function prcsed_order_2(){
// Get an instance of the WC_Order object
$order = new WC_Order( $customer_order );
foreach ( $customer_orders->orders as $customer_order ) {
return $order = $item->get_name();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强烈建议采取谨慎的态度来实施此解决方案。您发布的代码包含许多问题,并且它将处理敏感的客户数据。如果出错可能会导致数据泄露。
在回调
prcsed_order_2()
中,您尝试使用不存在的变量。您还会在 foreach 循环中返回,因此它永远不会超过第一个循环迭代。示例:
相同的原则可以应用于涉及客户订单数据的其他相关短代码。除了之前提出的安全问题之外,我还质疑以这种方式实现的性能影响。
I'd strongly advise taking a cautious approach to implementing this solution. The code you've posted contains a number of issues and it's going to be handling sensitive customer data. Getting this wrong could result in a data leak.
In the callback,
prcsed_order_2()
, you're attempting to use variables which don't exist. You're also returning in a foreach loop so it'll never make it past the first loop iteration.Example:
The same principles can be applied to other related shortcodes that touch upon customer order data. In addition to the security issue previously raised, I'd question the performance impact of implementing things in this manner.
这里有大量错误,FOR 循环中的返回只会返回第一次迭代,以及似乎没有初始化的变量等......但为了测试,
您可能会调用短代码来进行早期尝试;我只是为了自己的测试而调整了方法
Huge amount of errors here, the return in the FOR loop would just return the 1st iteration, and variables that dont seem to be initialised etc.... but for testing
You may be calling the shortcode to early try; I tweaked methods just for my own testing