WooCommerce Get Item Meta from All Orders

后端 未结 2 484
日久生厌
日久生厌 2021-02-06 15:13

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

相关标签:
2条回答
  • 2021-02-06 15:44

    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.

    0 讨论(0)
  • 2021-02-06 15:54

    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;
    
    0 讨论(0)
提交回复
热议问题