简体   繁体   English

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

[英]How to get all orders of current user (for Orders.php page)

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.第一个返回 0 作为值,我不明白我错在哪里。 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我需要使用短代码,因为我正在重新创建 orders.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'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.在回调prcsed_order_2()中,您试图使用不存在的变量。 You're also returning in a foreach loop so it'll never make it past the first loop iteration.您还将在 foreach 循环中返回,因此它永远不会超过第一次循环迭代。

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.除了之前提出的安全问题之外,我还质疑以这种方式实现事物的性能影响。

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这里有大量错误,FOR 循环中的返回只会返回第一次迭代,以及似乎没有初始化的变量等......但用于测试

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;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM