如何获取当前用户的所有订单(针对Orders.php页面)

发布于 2025-01-20 06:12:13 字数 824 浏览 1 评论 0原文

我正在尝试构建几个短代码来显示一些信息,例如用户下的所有订单的名称和订单号。

这些是短代码。第一个返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2025-01-27 06:12:13

我强烈建议采取谨慎的态度来实施此解决方案。您发布的代码包含许多问题,并且它将处理敏感的客户数据。如果出错可能会导致数据泄露。


在回调 prcsed_order_2() 中,您尝试使用不存在的变量。您还会在 foreach 循环中返回,因此它永远不会超过第一个循环迭代。

示例:

// Give the callback function a clear and descriptive name.
function wpse_get_customer_order_names() {
  
    // Can you be certain the user is logged in at this stage...
    // Consider how you might want to validate the user.

    // Get all orders for the current user.
    $customer_orders = wc_get_orders([
        'customer_id' => get_current_user_id(),
    ]);

    // Transform the array of order objects into an array of order names.
    $order_names = array_map( function( $order ) {
        return $order->name;
    }, $customer_orders );

    // Return as a string, ready for output,
    return implode( ', ', $order_names );
}
add_shortcode( 'prcsed_order_name' , 'wpse_get_customer_order_names' );

相同的原则可以应用于涉及客户订单数据的其他相关短代码。除了之前提出的安全问题之外,我还质疑以这种方式实现的性能影响。

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:

// Give the callback function a clear and descriptive name.
function wpse_get_customer_order_names() {
  
    // Can you be certain the user is logged in at this stage...
    // Consider how you might want to validate the user.

    // Get all orders for the current user.
    $customer_orders = wc_get_orders([
        'customer_id' => get_current_user_id(),
    ]);

    // Transform the array of order objects into an array of order names.
    $order_names = array_map( function( $order ) {
        return $order->name;
    }, $customer_orders );

    // Return as a string, ready for output,
    return implode( ', ', $order_names );
}
add_shortcode( 'prcsed_order_name' , 'wpse_get_customer_order_names' );

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.

此岸叶落 2025-01-27 06:12:13

这里有大量错误,FOR 循环中的返回只会返回第一次迭代,以及似乎没有初始化的变量等......但为了测试,

您可能会调用短代码来进行早期尝试;我只是为了自己的测试而调整了方法

add_action('init', 'xp33221_add_custom_shortcode');

function xp33221_add_custom_shortcode()
{
    add_shortcode( 'prcsed_order_numb' , 'prcsed_order_1' );
    add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
}

function getCustOrder(int $orderNum): ?WC_Order
{
    $order = wc_get_order($orderNum);

    if ($order instanceof \WC_Order)) {
       return $order;
    }  

    return null;
}

function prcsed_order_1(): ?int
{
    $order = getCustOrder($customer_order);
    
    return $order instanceof \WC_Order 
        ? $order->get_order_number() 
        : $order;
}

function prcsed_order_2() null|mixed
{
    $order = getCustOrder($customer_order);
    
    return $order instanceof \WC_Order 
        ? $order->get_order_key() 
        : $order;   
}

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

add_action('init', 'xp33221_add_custom_shortcode');

function xp33221_add_custom_shortcode()
{
    add_shortcode( 'prcsed_order_numb' , 'prcsed_order_1' );
    add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
}

function getCustOrder(int $orderNum): ?WC_Order
{
    $order = wc_get_order($orderNum);

    if ($order instanceof \WC_Order)) {
       return $order;
    }  

    return null;
}

function prcsed_order_1(): ?int
{
    $order = getCustOrder($customer_order);
    
    return $order instanceof \WC_Order 
        ? $order->get_order_number() 
        : $order;
}

function prcsed_order_2() null|mixed
{
    $order = getCustOrder($customer_order);
    
    return $order instanceof \WC_Order 
        ? $order->get_order_key() 
        : $order;   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文