Woocommerce如何从已终止的订单列表中获取订单ID

时间:2016-03-31 21:28:21

标签: php woocommerce

我需要从终止订单中检索订单ID。使用此代码时: (我确实只是回应了解......在最终代码中不需要它)

<?php
    $customer_orders = get_posts( array(
                'numberposts' => -1,
                'meta_key'    => '_customer_user',
                'meta_value'  => get_current_user_id(),
                'post_type'   => 'shop_order',
                'post_status' => 'completed',
                    ) );

                 // echo for tests                 
                 echo "<pre>";
                 print_r($customer_orders);
                 echo "</pre>";

                 foreach($customer_orders as $item) {
                 echo the_title();
                 //echo item[ID];   I let this one commented because it doesn't work...but it's what I need !                                       
                    } ?>

我可以清楚地看到我需要的东西:来自[ID] =&gt;的9570和9559

Array
(
[0] => WP_Post Object
    (
        [ID] => 9570
        [post_author] => 1
        [post_date] => 2016-03-31 13:19:42
        [post_date_gmt] => 2016-03-31 11:19:42
        [post_content] => 
        [post_title] => Order – mars 31, 2016 @ 01:19  
        [post_excerpt] => 
        [post_status] => wc-completed

etc...

[1] => WP_Post Object
    (
        [ID] => 9559
        [post_author] => 1
        [post_date] => 2016-03-28 15:55:27
        [post_date_gmt] => 2016-03-28 13:55:27
        [post_content] => 
        [post_title] => Order – mars 28, 2016 @ 03:55  
        [post_excerpt] => 
        etc....

但是试图检查我需要的值我没有得到任何好处...代码不起作用。我做了很多尝试,但没有任何工作:

echo item[0]; or echo item[ID];

我哪里错了?我需要这两个值才能将它们放在下拉字段中。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

  1. the_title仅适用于The Loop内部,请尝试重构您的代码,如下所示

    foreach( $customer_orders as $item ) {
    
        setup_postdata( $item );
    
        echo the_title();
        echo the_ID();
    
        //echo item[ID];   I let this one commented because it doesn't work...but it's what I need !
    
     }
     wp_reset_postdata();
    
  2. 除非是拼写错误,否则您使用的item[0]item[ID]不正确,因为@paskl已正确指出,该变量是WP_Post类的对象,以访问其属性您需要使用->运算符,其次您错过了前面的$符号,因此item[0]应为$item[0]

    foreach( $customer_orders as $item ) {
    
        echo $item->ID;  //this will work
        echo $item->post_title; // and so will this
    
    }
    

答案 1 :(得分:1)

这很好......但是......

 <?php
                        $args = get_posts( array(
                'numberposts' => -1,
                'meta_key'    => '_customer_user',
                'meta_value'  => get_current_user_id(),
                'post_type'   => 'shop_order',                    
                'post_status' => 'completed',
                    ) );

                    foreach($args as $item) {
                    setup_postdata( $item );
                    echo "<pre>";
                    echo the_title() . " - " . $item->post_title . " - Order N&ordm; : " . $item->ID;
                    echo "</pre>";                      
                    }   
                    ?>

它让我感觉像是:

 The Title - Order – avril 1, 2016 @ 05:09   - Order Nº : 9573

 The Title - Order – mars 28, 2016 @ 03:55   - Order Nº : 9559

不幸的是,“标题”不是订单中包含的产品标题,而是我使用该代码的页面标题。

相关问题