Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从终止的订单列表中获取订单ID_Php_Woocommerce - Fatal编程技术网

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

Php 如何从终止的订单列表中获取订单ID,php,woocommerce,Php,Woocommerce,我需要从终止的订单中检索订单ID。使用此代码时: (我放了一个回音只是为了理解……在最终代码中不需要它) 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

我需要从终止的订单中检索订单ID。使用此代码时: (我放了一个回音只是为了理解……在最终代码中不需要它)

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....
但是,试图检查我需要的值,我没有得到什么好的…代码不工作。我尝试了很多,但都没有成功:

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();
我错在哪里?我需要这两个值将它们放在下拉字段中

谢谢你的帮助

  • 只在内部工作,请尝试如下重构代码

    foreach( $customer_orders as $item ) {
    
        echo $item->ID;  //this will work
        echo $item->post_title; // and so will this
    
    }
    
  • 除非是打字错误,否则您使用的是
    item[0]
    item[ID]
    ,这是不正确的,因为@paskl已正确指出,变量是WP\u Post类的对象,要访问其属性,您需要使用
    ->
    运算符,其次您缺少前面的
    $
    符号,因此
    item[0]
    应为
    $item[0]

     <?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
    

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

    变量的类型为
    WP\u Post
    。因此,请检查它是否有任何getter,以便您可以获得您的值。否则我会猜它是某种
    stdClass
    。它的调用方式与数组不同。
     <?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