Bali Angelo Cogerino
Bali Angelo Cogerino

Reputation: 57

Woocommerce how to get order ID from terminated order list

I need to retrieve order ID from terminated order. When using this code : (I did put an echo only to understand..will not need it on final code)

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

I can clearly see what I need : 9570 and 9559 from [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....

but trying to check the value I need I get nothing good...the code doesn't work. I did try a lot but nothing work:

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

Where am I wrong ? I needs those two value to put them in a drop down field.

Thanks for any help.

Upvotes: 2

Views: 159

Answers (2)

Bali Angelo Cogerino
Bali Angelo Cogerino

Reputation: 57

This is working fine ...but...

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

It returns for me someting like:

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

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

Unfotunately "The Title" is not the product title contains in the order but the page title where I am using the code.

Upvotes: 1

Anand Shah
Anand Shah

Reputation: 14913

  1. the_title works only inside The Loop, try refactoring your code like below

    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. Unless its a typo, you are using item[0] or item[ID] which is incorrect, because @paskl has correctly pointed out, the variable is an object of WP_Post class, to access its attributes you'll need to use -> operator and secondly you are missing the preceding $ sign , so item[0] should be $item[0]

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

Upvotes: 1

Related Questions