4
votes

I am trying to display all Order Items (with the Item Meta) for All completed orders from the WooCommerce plugin. I also want to limit the display to 10 order items only. I have figured out how to display ALL Order Items but cannot limit the number to 10. Here is the code I am currently using to display All Order Items:

$args = array(
                    'post_type' => 'shop_order',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'shop_order_status',
                            'field' => 'slug',
                            'terms' => array('completed')
                        )
                    )
                );

                $loop = new WP_Query( $args );

                while ( $loop->have_posts() ) : $loop->the_post();
                    $order_id = $loop->post->ID;
                    $order = new WC_Order($order_id);

                    foreach( $order->get_items() as $item ) {   

                        $date = $item['Booking Date'];
                        $time = $item['Booking Time'];
                        $fname = $item['First Name - First Name'];
                        $church = $item['Church Information - Church Name'];
                        $city = $item['Church Information - City'];
                        $state = $item['Church Information - State'];
                    ?>

                        <div class="wc-upcoming-booking">
                            <div class="wc-upcoming-time">
                                <span class="upcoming-hour"><?php echo $time; ?></span>
                                <span class="upcoming-date"><?php echo $date; ?></span>
                            </div>
                            <div class="wc-upcoming-details">
                                <?php echo $fname . ', ' . $church . ', ' . $city . ', ' . $state; ?>
                            </div>
                        </div>

                    <?php }

                endwhile;

This code queries ALL Completed Orders and then loops through ALL Order Items for each queried Order. Some Orders may have more than 1 Order Item so if I limit the Posts Per Page to 10 and all of those Orders have 5 Order Items than I will have a total of 50 Order Items displayed. I tried adding an "iterate" variable to the foreach loop to limit that but that only loops through the "5" Order Items for one Order and NOT the total "50" Order Items.

First, I need to ORDER all Order Items by the $date and $time variable (which I believe I can do by converting them to a Timestamp using the strtotime() function).

Second, I would like to display to ONLY the first 10 Order Items (based on the timestamp) from All Orders.

Any ideas how to modify this code to allow for this?

2

2 Answers

1
votes

Something like this should limit the amount of items displayed to 10:

<?php
$args = array(
    'post_type' => 'shop_order',
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'tax_query' => array(
        array(
            'taxonomy' => 'shop_order_status',
            'field' => 'slug',
            'terms' => array('completed')
        )
    )
);
$orders=get_posts($args);
$i=0;
foreach($orders as $o):
    if($i>10){
        break;
    }
    $order_id = $o->ID;
    $order = new WC_Order($order_id);
    foreach( $order->get_items() as $item ):
        if($i>10){
            break;
        }
        $i++;
        $date = $item['Booking Date'];
        $time = $item['Booking Time'];
        $fname = $item['First Name - First Name'];
        $church = $item['Church Information - Church Name'];
        $city = $item['Church Information - City'];
        $state = $item['Church Information - State'];
    ?>
        <div class="wc-upcoming-booking">
            <div class="wc-upcoming-time">
                <span class="upcoming-hour"><?php echo $time; ?></span>
                <span class="upcoming-date"><?php echo $date; ?></span>
            </div>
            <div class="wc-upcoming-details">
                <?php echo $fname . ', ' . $church . ', ' . $city . ', ' . $state; ?>
            </div>
        </div>
    <?php endforeach;
endforeach;?>

If I understand what you're looking for correctly that should do the trick.

1
votes

I don't think wordpress provide you such type of query. You have to build your own logic to do this. Hope this may help you...

$args = array(
   'post_type' => 'shop_order',
   'post_status' => 'publish',
   'posts_per_page' => -1,
   'tax_query' => array(
       array(
            'taxonomy' => 'shop_order_status',
            'field' => 'slug',
            'terms' => array('completed')
            )
          )
   );

         $count = 0;
         $loop = new WP_Query( $args );

            while ( $loop->have_posts() ) : $loop->the_post();
                $order_id = $loop->post->ID;
                $order = new WC_Order($order_id);

                foreach( $order->get_items() as $item ) {   

                    if( $count > 10){
                       break 2; //break both loops 
                    } 
                    $count++;
                    $date = $item['Booking Date'];
                    $time = $item['Booking Time'];
                    $fname = $item['First Name - First Name'];
                    $church = $item['Church Information - Church Name'];
                    $city = $item['Church Information - City'];
                    $state = $item['Church Information - State'];
                ?>

                    <div class="wc-upcoming-booking">
                        <div class="wc-upcoming-time">
                            <span class="upcoming-hour"><?php echo $time; ?></span>
                            <span class="upcoming-date"><?php echo $date; ?></span>
                        </div>
                        <div class="wc-upcoming-details">
                            <?php echo $fname . ', ' . $church . ', ' . $city . ', ' . $state; ?>
                        </div>
                    </div>

                <?php }

            endwhile;